how to draw constantly changing graphics - c#

Have not done this before (except in java, look how Steve McLeod fixed it), so obviously I suck at it. Here 64 pixels around current mouse position get drawn little bigger on a form. Problem is, that it's 'kind of' to slow, and I have no idea where to start fixing.
Besides that, I made a timer thread, that constantly calls update graphics when it's finished and a little fps like text, to show really how fast things are drawn.
Image example: (Image is from letter 'a' in "IntelliTrace" in Microsoft VS2010)
Source example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Zoom
{
public partial class Form1 : Form
{
static class dllRef
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetCursorPos(out Point lpPoint);
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);
[DllImport("gdi32.dll")]
static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);
// from http://www.pinvoke.net/default.aspx/gdi32/GetPixel.html
static public System.Drawing.Color getPixelColor(int x, int y) {
IntPtr hdc = GetDC(IntPtr.Zero);
uint pixel = GetPixel(hdc, x, y);
ReleaseDC(IntPtr.Zero, hdc);
Color color = Color.FromArgb((int)(pixel & 0x000000FF),
(int)(pixel & 0x0000FF00) >> 8,
(int)(pixel & 0x00FF0000) >> 16);
return color;
}
static public System.Drawing.Point getMousePosition() {
Point p = new Point();
GetCursorPos(out p);
return p;
}
}
public Form1() {
InitializeComponent();
this.Size = new Size(400,400);
this.Text="Image zoom";
this.Location = new Point(640, 0);
this.image = new Bitmap(320, 320);
this.timeRef = DateTime.Now;
this.BackColor = Color.White;
Timer t = new Timer();
t.Interval = 25;
t.Tick += new EventHandler(Timer_Tick);
t.Start();
}
public void Timer_Tick(object sender, EventArgs eArgs) {
this.Form1_Paint(this, new PaintEventArgs(this.CreateGraphics(), new Rectangle(0, 0, this.Width, this.Height)));
}
private bool isdone = true;
private int iter = 0;
private Bitmap image;
private DateTime timeRef;
private void Form1_Paint(object sender, PaintEventArgs e) {
if (isdone) {
isdone = false;
int step = 40;
Point p = dllRef.getMousePosition();
Pen myPen = new Pen(Color.Gray, 1);
SolidBrush myBrush = null;
Bitmap image2 = new Bitmap(320, 340);
Graphics gc = Graphics.FromImage(image2);
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
myBrush = new SolidBrush(dllRef.getPixelColor(p.X - 4 + x, p.Y - 4 + y));
gc.FillEllipse(myBrush, x * step, y * step, step - 3, step - 3);
gc.DrawEllipse(myPen, x * step, y * step, step - 3, step - 3);
}
}
StringBuilder sb = new StringBuilder();
sb.Append(iter)
.Append(" frames in ")
.Append(String.Format("{0:0.###}", ((DateTime.Now-this.timeRef).TotalMilliseconds)/1000))
.Append("s.");
gc.FillRectangle(new SolidBrush(this.BackColor), new Rectangle( 0, 320, 320, 40));
gc.DrawString(sb.ToString(),new Font("Arial", 12),new SolidBrush(Color.Black), 10, 320);
gc.Dispose();
isdone = true;
iter++;
image = image2;
}
e.Graphics.DrawImage(image, 35f, 15f);
}
}
}
After changes i made, this one is ~98% faster:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Zoom
{
public partial class Form1 : Form
{
static class dllRef
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetCursorPos(out Point lpPoint);
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);
[DllImport("gdi32.dll")]
static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);
// from http://www.pinvoke.net/default.aspx/gdi32/GetPixel.html
static public System.Drawing.Color getPixelColor(int x, int y) {
IntPtr hdc = GetDC(IntPtr.Zero);
uint pixel = GetPixel(hdc, x, y);
ReleaseDC(IntPtr.Zero, hdc);
Color color = Color.FromArgb((int)(pixel & 0x000000FF),
(int)(pixel & 0x0000FF00) >> 8,
(int)(pixel & 0x00FF0000) >> 16);
return color;
}
static public System.Drawing.Point getMousePosition() {
Point p = new Point();
GetCursorPos(out p);
return p;
}
}
public Form1() {
InitializeComponent();
this.Size = new Size(400,400);
this.Text="Image zoom";
this.Location = new Point(640, 0);
this.image = new Bitmap(320, 340);
this.timeRef = DateTime.Now;
this.BackColor = Color.White;
Timer t = new Timer();
t.Interval = 25;
t.Tick += new EventHandler(Timer_Tick);
t.Start();
}
public void Timer_Tick(object sender, EventArgs eArgs) {
this.Form1_Paint(this, new PaintEventArgs(this.CreateGraphics(), new Rectangle(0, 0, this.Width, this.Height)));
}
private bool isdone = true;
private int iter = 0;
private Bitmap image;
private DateTime timeRef;
private void Form1_Paint(object sender, PaintEventArgs e) {
if (isdone) {
isdone = false;
int step = 40;
Point p = dllRef.getMousePosition();
SolidBrush myBrush = null;
Bitmap hc = new Bitmap(8, 8);
using (Pen myPen = new Pen(Color.Gray, 1))
using (Graphics gc = Graphics.FromImage(image))
using (Graphics gf = Graphics.FromImage(hc))
{
gf.CopyFromScreen(p.X - 4, p.Y - 4, 0, 0, new Size(8, 8),
CopyPixelOperation.SourceCopy);
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 8; y++)
{
myBrush = new SolidBrush(hc.GetPixel(x, y));
gc.FillEllipse(myBrush, x * step, y * step, step - 3, step - 3);
gc.DrawEllipse(myPen, x * step, y * step, step - 3, step - 3);
}
}
double ts = ((DateTime.Now - this.timeRef).TotalMilliseconds) / 1000;
StringBuilder sb = new StringBuilder();
sb.Append(++iter).Append(" frames in ").Append(String.Format("{0:0.###}", ts)).Append("s.");
gc.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(0, 320, 320, 40));
gc.DrawString(sb.ToString(), new Font("Arial", 12), new SolidBrush(Color.Black), 10, 320);
}
isdone = true;
}
e.Graphics.DrawImage(image, 35f, 15f);
}
}
}

One thing that should speed things up is if you do the GetDC just once and get all of the pixels you need, then call ReleaseDC. So rather than:
for each pixel
GetDC
Read Pixel
ReleaseDC
You have:
GetDC
for each pixel
read pixel and store value
ReleaseDC
Then process the stored pixels.
That said, you're probably better off not using GetPixel at all, as I seem to remember it being terribly inefficient. I suspect you'd have better performance just grabbing the entire screen into a bitmap and getting the pixels from there. Perhaps the answer to this question will help you: Capture the Screen into a Bitmap

Related

CopyFromScreen and GetPixel performance issues [duplicate]

Okay, I am looking for a function or something that will read the color of a certain pixel on my monitor, and when that color is detected, another function will be enabled. I figure using RGB. All help appreciated. Thank You.
This is the most efficient: It grabs a pixel at the location of the cursor, and doesn't rely on only having one monitor.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
namespace FormTest
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool GetCursorPos(ref Point lpPoint);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int BitBlt(IntPtr hDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);
public Form1()
{
InitializeComponent();
}
private void MouseMoveTimer_Tick(object sender, EventArgs e)
{
Point cursor = new Point();
GetCursorPos(ref cursor);
var c = GetColorAt(cursor);
this.BackColor = c;
if (c.R == c.G && c.G < 64 && c.B > 128)
{
MessageBox.Show("Blue");
}
}
Bitmap screenPixel = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
public Color GetColorAt(Point location)
{
using (Graphics gdest = Graphics.FromImage(screenPixel))
{
using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero))
{
IntPtr hSrcDC = gsrc.GetHdc();
IntPtr hDC = gdest.GetHdc();
int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, location.X, location.Y, (int)CopyPixelOperation.SourceCopy);
gdest.ReleaseHdc();
gsrc.ReleaseHdc();
}
}
return screenPixel.GetPixel(0, 0);
}
}
}
Now, obviously, you don't have to use the cursor's current location, but this is the general idea.
EDIT:
Given the above GetColorAt function you can poll a certain pixel on the screen in a safe, performance friendly way like this:
private void PollPixel(Point location, Color color)
{
while(true)
{
var c = GetColorAt(location);
if (c.R == color.R && c.G == color.G && c.B == color.B)
{
DoAction();
return;
}
// By calling Thread.Sleep() without a parameter, we are signaling to the
// operating system that we only want to sleep long enough for other
// applications. As soon as the other apps yield their CPU time, we will
// regain control.
Thread.Sleep()
}
}
You can wrap that in a Thread if you want, or execute it from a Console application. "Whatever suits your fancy," I guess.
Most answers here use the very same source of that pixel (desktop dc).
The key function is GetPixel.
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetWindowDC(IntPtr window);
[DllImport("gdi32.dll", SetLastError = true)]
public static extern uint GetPixel(IntPtr dc, int x, int y);
[DllImport("user32.dll", SetLastError = true)]
public static extern int ReleaseDC(IntPtr window, IntPtr dc);
public static Color GetColorAt(int x, int y)
{
IntPtr desk = GetDesktopWindow();
IntPtr dc = GetWindowDC(desk);
int a = (int) GetPixel(dc, x, y);
ReleaseDC(desk, dc);
return Color.FromArgb(255, (a >> 0) & 0xff, (a >> 8) & 0xff, (a >> 16) & 0xff);
}
I think this is the cleanest and quickest way.
Note:
If you have modified the default text size among the Display Settings on Windows to increase readability on a high resolution display, the coordinate parameters of GetPixel() need to be adjusted the same way. For example, if the cursor location is (x,y) with 150% of text size on Windows 7, you need to call GetPixel(x*1.5, y*1.5) to get the color of the pixel under the cursor.
This function is shorter and can achieve the same result using System.Drawing, without Pinvoke.
Color GetColorAt(int x, int y)
{
Bitmap bmp = new Bitmap(1, 1);
Rectangle bounds = new Rectangle(x, y, 1, 1);
using (Graphics g = Graphics.FromImage(bmp))
g.CopyFromScreen(bounds.Location, Point.Empty, bounds.Size);
return bmp.GetPixel(0, 0);
}
Please check this two different functions I have used in one of my previous projects :
1) This function takes snapshot of Desktop
private void CaptureScreenAndSave(string strSavePath)
{
//SetTitle("Capturing Screen...");
Bitmap bmpScreenshot;
Graphics gfxScreenshot;
bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
gfxScreenshot = Graphics.FromImage(bmpScreenshot);
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
MemoryStream msIn = new MemoryStream();
bmpScreenshot.Save(msIn, System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[0], null);
msIn.Close();
byte[] buf = msIn.ToArray();
MemoryStream msOut = new MemoryStream();
msOut.Write(buf, 0, buf.Length);
msOut.Position = 0;
Bitmap bmpOut = new Bitmap(msOut);
try
{
bmpOut.Save(strSavePath, System.Drawing.Imaging.ImageFormat.Bmp);
//SetTitle("Capturing Screen Image Saved...");
}
catch (Exception exp)
{
}
finally
{
msOut.Close();
}
}
2) This function takes an image in input and calculates RGB average of pixel range given.
double GetRGBAverageForPixelRange( int istartRange, int iEndRange, Bitmap oBitmap )
{
double dRetnVal = 0 ;
Color oTempColor ;
int i, j ;
for( int iCounter = istartRange ; iCounter < iEndRange ; iCounter++ )
{
i = (iCounter % (oBitmap.Width));
j = ( iCounter / ( oBitmap.Width ) ) ;
if (i >= 0 && j >= 0 && i < oBitmap.Width && j < oBitmap.Height )
{
oTempColor = oBitmap.GetPixel(i, j);
dRetnVal = dRetnVal + oTempColor.ToArgb();
}
}
return dRetnVal ;
}
This two functions together might solve your problem. Happy Coding :)
EDIT : Please note that GetPixel is very slow function. I will think twice befor using it.
As far as I know the easiest way to do this is to:
take a screenshot
look at the bitmap and get the pixel color
Edit
There is probably no way to "wait" until the pixel changes to a certain color. Your program will probably have to just loop and check it every so often until it sees the color.
For example:
while(!IsPixelColor(x, y, color))
{
//probably best to add a sleep here so your program doesn't use too much CPU
}
DoAction();
EDIT 2
Here is some sample code you can modify. This code just changes the color of a label based on the current color in a given pixel. This code avoids the handle leak mentioned.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int BitBlt(IntPtr hDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);
Thread t;
int x, y;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
x = 20;
y = 50;
t = new Thread(update);
t.Start();
}
private void update()
{
Bitmap screenCopy = new Bitmap(1, 1);
using (Graphics gdest = Graphics.FromImage(screenCopy))
{
while (true)
{
//g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(256, 256));
using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero))
{
IntPtr hSrcDC = gsrc.GetHdc();
IntPtr hDC = gdest.GetHdc();
int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, x, y, (int)CopyPixelOperation.SourceCopy);
gdest.ReleaseHdc();
gsrc.ReleaseHdc();
}
Color c = Color.FromArgb(screenCopy.GetPixel(0, 0).ToArgb());
label1.ForeColor = c;
}
}
}
}
}
This line uses About 10 ms.
int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, location.X, location.Y, (int)CopyPixelOperation.SourceCopy);

Selecting a Screenshot isn't accurate in C# and WPF

i made a program for taking a schreenshot from a selected area on the screen but it isn't accurate and i can't figure out why. It's problably because of mouse coordinates but I don't know what I did wrong. Maybe some of you could figure it out. The screenshot is always off and it catches the area of the screen above the actual selection and therefore "cuts" the lower part of the selection . This is my code:
public partial class Selektiranje : Window
{
public double x;
public double y;
public double width;
public double height;
public bool isMouseDown = false;
public Selektiranje()
{
InitializeComponent();
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
isMouseDown = true;
x = e.GetPosition(null).X; //Selekcija Screenshota
y = e.GetPosition(null).Y;
}
public void Window_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (this.isMouseDown)
{
double curx = e.GetPosition(null).X;
double cury = e.GetPosition(null).Y;
System.Windows.Shapes.Rectangle r = new , System.Windows.Shapes.Rectangle();
SolidColorBrush brush = new SolidColorBrush(Colors.White);
r.Stroke = brush;
r.Fill = brush;
r.StrokeThickness = 1;
r.Width = Math.Abs(curx - x);
r.Height = Math.Abs(cury - y);
selekt.Children.Clear();
selekt.Children.Add(r);
Canvas.SetLeft(r, x);
Canvas.SetTop(r, y);
if (e.LeftButton == MouseButtonState.Released)
{
selekt.Children.Clear();
width = e.GetPosition(null).X - x;
height = e.GetPosition(null).Y - y;
this.CaptureScreen(x, y, width, height);
this.x = this.y = 0;
this.isMouseDown = false;
this.Close();
}
}
}
public void CaptureScreen(double x, double y, double width, double height)
{
int ix, iy, iw, ih;
ix = Convert.ToInt32(x);
iy = Convert.ToInt32(y);
iw = Convert.ToInt32(width);
ih = Convert.ToInt32(height);
Bitmap slika = new Bitmap(iw, ih, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(slika);
g.CopyFromScreen(ix, iy, 0, 0,new System.Drawing.Size(iw, ih),CopyPixelOperation.SourceCopy);
public void SaveScreen(double x, double y, double width, double height)
{
int ix, iy, iw, ih;
ix = Convert.ToInt32(x);
iy = Convert.ToInt32(y);
iw = Convert.ToInt32(width);
ih = Convert.ToInt32(height);
try
{
Bitmap slika = new Bitmap(iw, ih);
Graphics gr1 = Graphics.FromImage(slika);
IntPtr dc1 = gr1.GetHdc();
IntPtr dc2 = NativeMethods.GetWindowDC(NativeMethods.GetForegroundWindow());
NativeMethods.BitBlt(dc1, ix, iy, iw, ih, dc2, ix, iy, 13369376);
gr1.ReleaseHdc(dc1);
System.Windows.Forms.SaveFileDialog dlg = new System.Windows.Forms.SaveFileDialog();
dlg.DefaultExt = "png";
dlg.Filter = "Png Files|*.png";
DialogResult res = dlg.ShowDialog();
if (res == System.Windows.Forms.DialogResult.OK)
slika.Save(dlg.FileName, ImageFormat.Png);
}
catch
{
}
}
internal class NativeMethods
{
[DllImport("user32.dll")]
public extern static IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hwnd);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
[DllImport("gdi32.dll")]
public static extern UInt64 BitBlt(IntPtr hDestDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, System.Int32 dwRop);
}
}
}
You need Cursor.Position (MSDN) in Window_MouseDown() and Window_MouseMove() which returns absolute mouse coordinates.
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
isMouseDown = true;
x = Cursor.Position.X;
y = Cursor.Position.Y;
}
Code of a fully working solution (for me) below. I removed unnecessary code such as drawing the rectangle. In addition, I hide the form before taking the screenshot. You could also set the form's background color to a specific value and set the transparency key to the same value or something.
But the basic concept remains: use absolute screen coordinates.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media;
using PixelFormat = System.Drawing.Imaging.PixelFormat;
using Rectangle = System.Windows.Shapes.Rectangle;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public double x;
public double y;
public double width;
public double height;
public bool isMouseDown;
public Form1()
{
InitializeComponent();
}
public void SaveScreen(double x, double y, double width, double height)
{
var ix = Convert.ToInt32(x);
var iy = Convert.ToInt32(y);
var iw = Convert.ToInt32(width);
var ih = Convert.ToInt32(height);
try
{
var slika = new Bitmap(iw, ih, PixelFormat.Format32bppArgb);
var g = Graphics.FromImage(slika);
g.CopyFromScreen(ix, iy, 0, 0, new Size(iw, ih), CopyPixelOperation.SourceCopy);
var dlg = new SaveFileDialog
{
DefaultExt = "png",
Filter = "Png Files|*.png"
};
var res = dlg.ShowDialog();
if (res == DialogResult.OK) slika.Save(dlg.FileName, ImageFormat.Png);
}
catch
{
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
isMouseDown = true;
x = Cursor.Position.X;
y = Cursor.Position.Y;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
width = Cursor.Position.X - x;
height = Cursor.Position.Y - y;
Hide();
Size = new Size(0, 0);
Application.DoEvents();
SaveScreen(x, y, width, height);
x = y = 0;
isMouseDown = false;
Close();
}
}
}
Graphics.CopyFromScreen needs the screen coordinates to take picture. You are passing coorduinates relative to form because of using e.x and e.y of MouseEventArgs. You should use screen coordinates of mouse instead, using Cursor.Position or MousePosition that are identical.
If you are using WPF
There are many options that may help you to get screen coordinates, some of those options:
Option 1
You can use PointToScreen method to convert the coordinates to screen coordinates.
Option 2
In WPF you cant use those methods, the most simple way would be add a reference to System.Windows.Forms.dll to your WPF project and then use System.Windows.Forms.Control.MousePosition which is static.
Option 3
As another option you can add a reference to System.Drawing.dll and use this:
[System.Runtime.InteropServices.DllImport("user32.dll")]
[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
internal static extern bool GetCursorPos(ref PointStruct point);
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
internal struct PointStruct
{
public Int32 X;
public Int32 Y;
};
public static System.Drawing.Point MousePosition()
{
var mousePosition = new PointStruct();
GetCursorPos(ref mousePosition);
return new System.Drawing.Point(mousePosition.X, mousePosition.Y);
}
Then you can use MousePosition() to get the current position of mouse on screen.

Add a UAC shield to a button and retain its background image?

Using C# and .Net 4.0 in a winforms application: Is it possible to add the UAC shield to a button and retain the buttons background image? How?
This is what I'm using at the moment, but it removes the image...
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
public static void UACToButton(Button button, bool add)
{
const Int32 BCM_SETSHIELD = 0x160C;
if (add)
{
// The button must have the flat style
button.FlatStyle = FlatStyle.System;
if (button.Text == "")
// and it must have text to display the shield
button.Text = " ";
SendMessage(button.Handle, BCM_SETSHIELD, 0, 1);
}
else
SendMessage(button.Handle, BCM_SETSHIELD, 0, 0);
}
Thanks!
As Elmue mentioned, SystemIcons.Shield is the easiest way to access the UAC Shield icon. Here's the method that I use to add it on top of other images:
public static Image AddUACShieldToImage(Image image)
{
var shield = SystemIcons.Shield.ToBitmap();
shield.MakeTransparent();
var g = Graphics.FromImage(image);
g.CompositingMode = CompositingMode.SourceOver;
g.DrawImage(shield, new Rectangle(image.Width / 2, image.Height / 2, image.Width / 2, image.Height / 2));
return image;
}
Perhaps this will help: http://blog.csharphelper.com/2011/03/03/add-uac-shields-to-buttons-menu-items-and-picture-boxes-in-c.aspx
It takes the UAC shield and returns a bitmap that can be placed on anything that supports bitmaps.
EDIT:
Here's some rough sample code that could work:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace UAC_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
// Make the button display the UAC shield.
public static void AddShieldToButton(Button btn)
{
const Int32 BCM_SETSHIELD = 0x160C;
// Give the button the flat style and make it display the UAC shield.
btn.FlatStyle = System.Windows.Forms.FlatStyle.System;
SendMessage(btn.Handle, BCM_SETSHIELD, 0, 1);
}
// Return a bitmap containing the UAC shield.
private static Bitmap shield_bm = null;
public static Bitmap GetUacShieldImage()
{
if (shield_bm != null) return shield_bm;
const int WID = 50;
const int HGT = 50;
const int MARGIN = 4;
// Make the button. For some reason, it must
// have text or the UAC shield won't appear.
Button btn = new Button();
btn.Text = " ";
btn.Size = new Size(WID, HGT);
AddShieldToButton(btn);
// Draw the button onto a bitmap.
Bitmap bm = new Bitmap(WID, HGT);
btn.Refresh();
btn.DrawToBitmap(bm, new Rectangle(0, 0, WID, HGT));
// Find the part containing the shield.
int min_x = WID, max_x = 0, min_y = HGT, max_y = 0;
// Fill on the left.
for (int y = MARGIN; y < HGT - MARGIN; y++)
{
// Get the leftmost pixel's color.
Color target_color = bm.GetPixel(MARGIN, y);
// Fill in with this color as long as we see the target.
for (int x = MARGIN; x < WID - MARGIN; x++)
{
// See if this pixel is part of the shield.
if (bm.GetPixel(x, y).Equals(target_color))
{
// It's not part of the shield.
// Clear the pixel.
bm.SetPixel(x, y, Color.Transparent);
}
else
{
// It's part of the shield.
if (min_y > y) min_y = y;
if (min_x > x) min_x = x;
if (max_y < y) max_y = y;
if (max_x < x) max_x = x;
}
}
}
// Clip out the shield part.
int shield_wid = max_x - min_x + 1;
int shield_hgt = max_y - min_y + 1;
shield_bm = new Bitmap(shield_wid, shield_hgt);
Graphics shield_gr = Graphics.FromImage(shield_bm);
shield_gr.DrawImage(bm, 0, 0,
new Rectangle(min_x, min_y, shield_wid, shield_hgt),
GraphicsUnit.Pixel);
// Return the shield.
return shield_bm;
}
private void Form1_Load(object sender, EventArgs e)
{
Graphics gfx = Graphics.FromImage(button1.BackgroundImage);
Bitmap shield = GetUacShieldImage();
gfx.DrawImage(shield, button1.Width / 2, button1.Height / 2);
}
}
}
This is drawing the UAC shield over the background image. You can adjust the image location on top of the button. It doesn't look as pretty as the System UAC shield, but it does show the UAC shield icon on top of your background image.

High CPU usage when getting color under cursor

I'm working on an app, moving the mouse in a certain area and clicking the mouse if something is not the color black.
However, I am getting a really high CPU usage while using this method too get the color below the cursor. After 5 completed runs from startY to endY - the application is lagging that much it takes around 5-10 sec. to get too the end of the area. With this part commented out, the application runs fine and each run doesn't increase in the too complete.
Here is my while loop:
private void moveMouse(int startX, int endX, int startY, int endY)
{
int newPosX = startX;
int newPosY = startY;
while (running)
{
Application.DoEvents();
//this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(newPosX, newPosY);
Thread.Sleep(3);
if (colorCursor.Get(newPosX, newPosY))
{
MyMouse.sendClick();
countClicks++;
lblStatus.Text = "Klik: " + countClicks;
}
newPosX += 10;
if (newPosX > endX)
{
newPosY += 25;
newPosX = startX;
}
if (newPosY > endY)
{
newPosY = startY;
Thread.Sleep(1000);
}
}
}
Color below cursor:
public class ColorUnderCursor
{
[DllImport("gdi32")]
public static extern uint GetPixel(IntPtr hDC, int XPos, int YPos);
//[DllImport("user32.dll", CharSet = CharSet.Auto)]
//public static extern bool GetCursorPos(out POINT pt);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
public bool Get(int x, int y)
{
IntPtr dc = GetWindowDC(IntPtr.Zero);
long color = GetPixel(dc, x, y);
Color underMouse = Color.FromArgb((int)color);
if(underMouse != Color.FromArgb(0, 0, 0, 0))
return true;
return false;
}
}
How I can minimize this heavy usage of the CPU.
Solution:
It was my method "Get" which was causing the problem. I solved it by this method below, and running the whole thing inside a backgroundworker.
public bool GetPixel(Point position)
{
using (var bitmap = new Bitmap(1, 1))
{
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(position, new Point(0, 0), new Size(1, 1));
}
if (bitmap.GetPixel(0, 0) != Color.FromArgb(255, 0, 0, 0) && bitmap.GetPixel(0, 0) != Color.FromArgb(255, 255, 255, 255))
return true;
return false;
}
}
Try to move logic into Invoke call like this
private void moveMouse(int startX, int endX, int startY, int endY)
{
this.BeginInvoke(new Action(() => { InvokeMouseMove(startX, endX, startY, endY)
}));
}
private void InvokeMouseMove(int startX, int endX, int startY, int endY)
{
int newPosX = startX;
int newPosY = startY;
while (running)
{
Application.DoEvents();
//this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(newPosX, newPosY);
if (colorCursor.Get(newPosX, newPosY))
{
MyMouse.sendClick();
countClicks++;
lblStatus.Text = "Klik: " + countClicks;
}
newPosX += 10;
if (newPosX > endX)
{
newPosY += 25;
newPosX = startX;
}
if (newPosY > endY)
{
newPosY = startY;
}
}
}

How to read the Color of a Screen Pixel

Okay, I am looking for a function or something that will read the color of a certain pixel on my monitor, and when that color is detected, another function will be enabled. I figure using RGB. All help appreciated. Thank You.
This is the most efficient: It grabs a pixel at the location of the cursor, and doesn't rely on only having one monitor.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
namespace FormTest
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool GetCursorPos(ref Point lpPoint);
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int BitBlt(IntPtr hDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);
public Form1()
{
InitializeComponent();
}
private void MouseMoveTimer_Tick(object sender, EventArgs e)
{
Point cursor = new Point();
GetCursorPos(ref cursor);
var c = GetColorAt(cursor);
this.BackColor = c;
if (c.R == c.G && c.G < 64 && c.B > 128)
{
MessageBox.Show("Blue");
}
}
Bitmap screenPixel = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
public Color GetColorAt(Point location)
{
using (Graphics gdest = Graphics.FromImage(screenPixel))
{
using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero))
{
IntPtr hSrcDC = gsrc.GetHdc();
IntPtr hDC = gdest.GetHdc();
int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, location.X, location.Y, (int)CopyPixelOperation.SourceCopy);
gdest.ReleaseHdc();
gsrc.ReleaseHdc();
}
}
return screenPixel.GetPixel(0, 0);
}
}
}
Now, obviously, you don't have to use the cursor's current location, but this is the general idea.
EDIT:
Given the above GetColorAt function you can poll a certain pixel on the screen in a safe, performance friendly way like this:
private void PollPixel(Point location, Color color)
{
while(true)
{
var c = GetColorAt(location);
if (c.R == color.R && c.G == color.G && c.B == color.B)
{
DoAction();
return;
}
// By calling Thread.Sleep() without a parameter, we are signaling to the
// operating system that we only want to sleep long enough for other
// applications. As soon as the other apps yield their CPU time, we will
// regain control.
Thread.Sleep()
}
}
You can wrap that in a Thread if you want, or execute it from a Console application. "Whatever suits your fancy," I guess.
Most answers here use the very same source of that pixel (desktop dc).
The key function is GetPixel.
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetWindowDC(IntPtr window);
[DllImport("gdi32.dll", SetLastError = true)]
public static extern uint GetPixel(IntPtr dc, int x, int y);
[DllImport("user32.dll", SetLastError = true)]
public static extern int ReleaseDC(IntPtr window, IntPtr dc);
public static Color GetColorAt(int x, int y)
{
IntPtr desk = GetDesktopWindow();
IntPtr dc = GetWindowDC(desk);
int a = (int) GetPixel(dc, x, y);
ReleaseDC(desk, dc);
return Color.FromArgb(255, (a >> 0) & 0xff, (a >> 8) & 0xff, (a >> 16) & 0xff);
}
I think this is the cleanest and quickest way.
Note:
If you have modified the default text size among the Display Settings on Windows to increase readability on a high resolution display, the coordinate parameters of GetPixel() need to be adjusted the same way. For example, if the cursor location is (x,y) with 150% of text size on Windows 7, you need to call GetPixel(x*1.5, y*1.5) to get the color of the pixel under the cursor.
This function is shorter and can achieve the same result using System.Drawing, without Pinvoke.
Color GetColorAt(int x, int y)
{
Bitmap bmp = new Bitmap(1, 1);
Rectangle bounds = new Rectangle(x, y, 1, 1);
using (Graphics g = Graphics.FromImage(bmp))
g.CopyFromScreen(bounds.Location, Point.Empty, bounds.Size);
return bmp.GetPixel(0, 0);
}
Please check this two different functions I have used in one of my previous projects :
1) This function takes snapshot of Desktop
private void CaptureScreenAndSave(string strSavePath)
{
//SetTitle("Capturing Screen...");
Bitmap bmpScreenshot;
Graphics gfxScreenshot;
bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
gfxScreenshot = Graphics.FromImage(bmpScreenshot);
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
MemoryStream msIn = new MemoryStream();
bmpScreenshot.Save(msIn, System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[0], null);
msIn.Close();
byte[] buf = msIn.ToArray();
MemoryStream msOut = new MemoryStream();
msOut.Write(buf, 0, buf.Length);
msOut.Position = 0;
Bitmap bmpOut = new Bitmap(msOut);
try
{
bmpOut.Save(strSavePath, System.Drawing.Imaging.ImageFormat.Bmp);
//SetTitle("Capturing Screen Image Saved...");
}
catch (Exception exp)
{
}
finally
{
msOut.Close();
}
}
2) This function takes an image in input and calculates RGB average of pixel range given.
double GetRGBAverageForPixelRange( int istartRange, int iEndRange, Bitmap oBitmap )
{
double dRetnVal = 0 ;
Color oTempColor ;
int i, j ;
for( int iCounter = istartRange ; iCounter < iEndRange ; iCounter++ )
{
i = (iCounter % (oBitmap.Width));
j = ( iCounter / ( oBitmap.Width ) ) ;
if (i >= 0 && j >= 0 && i < oBitmap.Width && j < oBitmap.Height )
{
oTempColor = oBitmap.GetPixel(i, j);
dRetnVal = dRetnVal + oTempColor.ToArgb();
}
}
return dRetnVal ;
}
This two functions together might solve your problem. Happy Coding :)
EDIT : Please note that GetPixel is very slow function. I will think twice befor using it.
As far as I know the easiest way to do this is to:
take a screenshot
look at the bitmap and get the pixel color
Edit
There is probably no way to "wait" until the pixel changes to a certain color. Your program will probably have to just loop and check it every so often until it sees the color.
For example:
while(!IsPixelColor(x, y, color))
{
//probably best to add a sleep here so your program doesn't use too much CPU
}
DoAction();
EDIT 2
Here is some sample code you can modify. This code just changes the color of a label based on the current color in a given pixel. This code avoids the handle leak mentioned.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int BitBlt(IntPtr hDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);
Thread t;
int x, y;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
x = 20;
y = 50;
t = new Thread(update);
t.Start();
}
private void update()
{
Bitmap screenCopy = new Bitmap(1, 1);
using (Graphics gdest = Graphics.FromImage(screenCopy))
{
while (true)
{
//g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(256, 256));
using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero))
{
IntPtr hSrcDC = gsrc.GetHdc();
IntPtr hDC = gdest.GetHdc();
int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, x, y, (int)CopyPixelOperation.SourceCopy);
gdest.ReleaseHdc();
gsrc.ReleaseHdc();
}
Color c = Color.FromArgb(screenCopy.GetPixel(0, 0).ToArgb());
label1.ForeColor = c;
}
}
}
}
}
This line uses About 10 ms.
int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, location.X, location.Y, (int)CopyPixelOperation.SourceCopy);

Categories