I want to search for an image on screen using C# or other .NET languages(like powershell). Something like i give an image location in the file system and the code consider the whole screen as an image and search the image in the file system in the big image(the screen) then returns the image position on screen. I can't find this kind of things in the .net classes.
Thanks.
This is a pretty specific problem, which is why you won't find it in the .NET Framework. You should break down your problem in smaller pieces:
Load image from file on disk
Use System.Drawing.Image.FromFile().
Acquire an image of the screen, i.e. a screen shot
Use System.Drawing.Graphics.CopyFromScreen():
Bitmap CaptureScreen()
{
var image = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
var gfx = Graphics.FromImage(image);
gfx.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
return image;
}
Find image inside image
See answer to this question.
i have made a program which used a lib i coded in c# which will return the value of x and y of a small image inside a bigger image (screenshot) you can see all the details here https://www.nulled.io/topic/22223-an-advanced-image-search-library-saeedsearchdll/
Related
I'm writing a little program that tranfers a screen shot of a user screen to my wpf image control but when the image is displayed it is not full,not the whole screen is displayed even if the action was done through my computer and not my leptop and it is blurry
Bitmap bmp=
new Bitmap((int)SystemParameters.PrimaryScreenWidth(int)SystemParameters.PrimaryScreenHeight);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(0, 0, 0, 0, bmp.Size);
the code above is the code to take the screenshot
BitmapImage bmI = new BitmapImage();
bmI.BeginInit();
bmI.UriSource = new Uri(FullPath);
bmI.EndInit();
Screen_Shot.Source = bmI;
And this code is the code to dislpay the image, Screen_Shot is the image name, and the full path is where i put the bitmap image
I tried to use
Screen_Shot.Stretch = Stretch.Fill; and UseLayoutRounding="True" SnapsToDevicePixels="True"
but none of them seems to get the job's done
this is an exsample of a screenshot i took from my own pc
You want to set the Stretch property to Uniform or else your image will be distorted as it will fill regardless of the Image control's size/shape. You also have to remember that the screen size may be different than your programs window size. If you don't uniformly stretch the image it will distort almost always (even if it's just a bit).
Regarding your "blurry" image, you need to specify the rendering option of you Screen_Shot image control or it will set the image to a lower quality that it can render more easily. This is honestly better to just set in the xaml. I normally use Fant, but look other options specified here.
<Image Name="Screen_Shot" RenderOption.BitmapScalingMode="Fant"/>
I am making an windows form application in which i am overlaying an image during a webcam stream from the laptop.
the overlayed image is showing absurd colours. it is showing alot of pink colour. is there anything i can do to make the overlayed image look properly.
i am using the camera_Net Library to connect to the webcam
suggestions for overlaying an image during during a webcam video shall also be appreciated.
here is my code to draw the image
string filepath = #"E:\office\lux desktop app\Camera_Net-master\Camera_Net-master\Samples\CameraControlTool\water_PNG3290.png";
Bitmap bitmap1 = new Bitmap(filepath);
g.DrawImage(bitmap1, new Rectangle(400 , 0, 250, 600));
here is the look of the image during webcam stream
and here is the orignal image being overlayed
I think the problem is the image itself. It's an .png image, so I it has the possibility of an alpha-value. The white in the back is cropped out but in between the water, the light blue values are pink.
I would first try to use an easier image. Something like this. I think this will work just fine. Then search for some more complex images and try to find the weak-spot and find some alternative options to bring the image in to the cam.
As #Roman R pointed out that g point correctly to an incorrect background,
the problem was indeed with color format, so the solution is to use the correct image pixelformat based on your image.
here is the complete code
Bitmap bmp = new Bitmap(w, h, PixelFormat.Format32bppRgb);
Graphics g = Graphics.FromImage(bmp);
Image newImage;
newImage = Properties.Resources.red_frame_03;
using (Bitmap oldBmp = new Bitmap(newImage))
using (Bitmap newBmp = new Bitmap(oldBmp))
using (Bitmap targetBmp = newBmp.Clone(new Rectangle(0, 0, newBmp.Width, newBmp.Height), PixelFormat.Format16bppArgb1555))
{
g.DrawImage(targetBmp, new Rectangle(100, 0, 350, 350));
}
I want to make very simplistic paint/image editor. Mainly, for pixel editing, but that doesn't seem relevant.
To ease up my effort, I decided to keep the image size at 16x16.
I populate the form, add a PixelBox and slap a default image on it.
Of course, I need to make the pixels visible, set the interpolation to NearestNeighbor.
Then, I stretch the pixelbox to 320x320. And there the situation arises.
The image is displayed as thus:
Cropped image
Could someone shed some light on this? This is just a 16x16 image with a checkerboard pattern that I made, but I can't figure out why it is displayed with that offset at the top left.
Also, no code as been yet added. I assume this is default behavior?
If you look at the examples on the page that exact same error happens, so it must be a bug on the PixelBox.
Instead of using a custom control for this type of operation just use the standard PictureBox and scale the image by yourself:
public Bitmap ScaleBitmap(Bitmap src, Size NewSize)
{
Bitmap bmp = new Bitmap(NewSize.Width, NewSize.Height, src.PixelFormat);
Graphics g = Graphics.FromImage(src);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.DrawImage(src, new Rectangle(Point.Empty, NewSize), new Rectangle(0, 0, src.Width, src.Height), GraphicsUnit.Pixel);
g.Dispose();
return bmp;
}
I have a problem adding cliparts (from png files) to bmp object. I add some cliparts to a jpeg image and after I save it, I get a mistery: some cliparts are present in saved image, but some not.
Here is code how I add cliparts to image:
using (System.Drawing.Graphics gfx = System.Drawing.Graphics.FromImage(this._image))
{
gfx.CompositingMode = CompositingMode.SourceOver;
gfx.CompositingQuality = CompositingQuality.HighQuality;
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.DrawImage(image, posX, posY, newWidth, newHeight);
gfx.Save();
}
Can anybode explain what am I doing wrong or why this situation appears?
edited:
it happens when I use this code inside a ASP.NET Application, when I use it in windows application everything is fine
I'm not sure if this is the cause of Your issue, but line gfx.Save() does not save the image you painted back to the image, but saves state of the Graphics object. In order to save the changes back to the image, make sure to call Image.Save() method and double check to make sure You are not restoring the previous image somewhere in the code. More information about it Graphics.Save method can be found here: http://msdn.microsoft.com/en-us/library/system.drawing.graphics.save(v=vs.100).aspx.
Update
You wrote in the comments that it's a transparency issue. If this is the cause, take a look at the code loading the clipart images from files. This also may be caused by various pixel formats used in your image files. Changing You images loading algorithm to something like this may solve the issue:
load your background image, let's say it's done like this: Image backgroundImg = new Bitmap(backgroundImgPath);
create a new empty image as a base for all the images: this._image = new Bimap(backgroundImg.Width, backgroundImg.Height, PixelFormat.Format32bppArgb);
draw the contents of backgroundImg onto this._image with gfx.CompositingMode = CompositingMode.SourceCopy;
then, you can draw the cliparts as You're doing it now (just comment out gfx.Save(); method)
Let me know if this helped.
I need to create an application in C# that captures part of the screen when certain part of the same screen changes. Thank you all.
You can use the System.Drawing.Graphics class. It has a CopyFromScreen method that will draw the content of a rectangular area of the screen into a Bitmap object.
It should do what you are after.
Have a look at this open source project called - Cropper. It is developed using C#.
Download the source code and have a look at it, you will get the basic idea of using System.Drawing classes.
Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(Left, Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
bmp.Save(fileName, ImageFormat.Jpeg);