Partial Screen Capture using C# - c#

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);

Related

How can I plot pixels onto the screen without WinForms using F#/C#?

I want to know how to get and set pixel that are currently shown on the screen, I'm sure this must be possible I just don't know how. Would I need to use a DLL? I don't have any code to show at this point but I just needed to be pointed in the right direction, I can for the most part port from C# to F# if you can only answer in C#.
Look at this question:
How to read screen pixels for an application that is not in the foreground?
using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height))
{
using (Graphics g = Graphics.FromImage(bmpScreenCapture))
{
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, bmpScreenCapture.Size, CopyPixelOperation.SourceCopy);
}
Color c = bmpScreenCapture.GetPixel(x,y);
}

image overlay picture from Camer_net library showing a lot of pink color

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));
}

Draw the change portion of image on another image

suppose i have two images img1.jpg and img2.jpg. using some routine i could extract the difference between two images. now difference is saved in another bitmap variable called diff
here is code
Bitmap diff = new Bitmap(bounds.Width, bounds.Height);
Graphics g = Graphics.FromImage(diff);
g.DrawImage(secondImg, 0, 0, bounds, GraphicsUnit.Pixel);
g.Dispose();
i know the difference in terms of rectangle and also save the difference in diff variable. now i want to merge or draw this difference on my first image. i tried with code like
Graphics g1 = Graphics.FromImage(firstImg);
g1.DrawImage(secondImg, 0, 0, bounds, GraphicsUnit.Pixel);
g1.Dispose();
but it is not working because when i open my first image img1.jpg then i am seeing any changes in that image. i want to draw the changes on my first image img1.jpg. what is wrong in my code which is not being able to dump or draw the changes on first image.
basically i have to reconstruct img1, if i have img2 and difference between img2 and img1.
please guide me. thanks

How to search for an image on screen in C#?

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/

Resize transparent images in windows mobile using C#

Is there a way to re-size a transparent image in windows mobile(C#)? I can re-size the image but I am losing the transparency. It is being replaced with white.
Here is what I have now
public static void ResizePicture(string imageFileName, Size maxSize)
{
using (Image src = new Bitmap(imageFileName))
{
using (Bitmap dst = new Bitmap(maxSize.Width, maxSize.Height))
{
using (Graphics g = Graphics.FromImage(dst))
{
ImageAttributes imageAttr = new ImageAttributes();
g.Clear(Color.Transparent);
g.DrawImage(src, new Rectangle(0, 0, dst.Width, dst.Height), 0, 0, src.Width, src.Height, GraphicsUnit.Pixel, imageAttr);
}
dst.Save(imageFileName, ImageFormat.Png);
}
}
}
Have a look at this as found in the MSDN about setting the transparency color. It looks like you need to set the SetColorKey for this to work. See also here that explains you cannot set the transparency (apparently contradicting the first link). But, you can still down the Smart Devices Framework (community edition) found in OpenNetCF.org and use that instead to handle the transparency workaround. I would try the first link before going any further to see if that works.
Hope this helps,
Best regards,
Tom.
The CF has, in my opinion anyway, a bug. Graphics.Clear with Color.Transparent actually fills the image with white, not transparent. The workaround is tedious. See the link above.

Categories