Draw the change portion of image on another image - c#

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

Related

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

C# Zoomed image in Picturebox gets cropped in top left

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

Replace part of an image with another in C# with WinForms

I have a big .PNG that has many little images on it. I want to replace part of the big image with a smaller one. So at X and Y coordinates, that part of the image will be replaced starting from the top left hand corner, while still leaving the rest of the original image intact.
I have been reading about the Graphics methods on MSDN and also had a look for some examples of a similar thing but didn't find much.
Had anyone done anything similar?
Thanks!
I would suggest this approach. X and Y are the coordinates on the big image where you want to put the small one. You can check the DrawImage method overloads, there are 30 of them but I think this one best suites your case:
Bitmap bigBmp = new Bitmap("bigBmp.png");
Bitmap smallBmp = new Bitmap("smallBmp.png");
Graphics g = Graphics.FromImage(bigBmp);
Rectangle destRect = new Rectangle(x, y, smallBmp.Width, smallBmp.Height);
Rectangle sourceRect = new Rectangle(0, 0, smallBmp.Width, smallBmp.Height);
g.DrawImage(smallBmp, destRect, sourceRect, GraphicsUnit.Pixel);
g.Dispose();
EDIT: Based on the comment of KvanTTT, I have decided to add another solution to the question using DrawImageUnscaled because it is the fastest way to draw images. There are four overloads of this method, but this one is the simplest one that matches the question.
Bitmap bigBmp = new Bitmap("bigBmp.png");
Bitmap smallBmp = new Bitmap("smallBmp.png");
Graphics g = Graphics.FromImage(bigBmp);
g.DrawImageUnscaled(smallBmp, x, y);
g.Dispose();

bit map scaling

I have bitmap of 24*24 pixels, I want to resize the bitmap dynamically 48*48 pixels or 5*5 pixels. How to do this with out losing the information
You could try something like this
Bitmap bm = new Bitmap(#"C:\Test1.bmp");
Bitmap result = new Bitmap(48, 48);
using (Graphics g = Graphics.FromImage(result))
g.DrawImage(bm, 0, 0, 48, 48);
result.Save(#"C:\Test2.bmp");
Typically scaling/Resizing images does change the quality of the new image...
You will lose information. To give the impression you are not losing as much information you need to use anti-aliasing. Iterate over the original image and consolidate the pixels which will become one pixel in your final image by taking the average of their colours.

How to render images in real-time

I am looking for a solution\software that I can use in order to render buttons in real time.
That is, I have an image and text, and I want them to be an image altogether in realtime.
Thank you
You can dynamically create an image based on text using the System.Drawing namespace.
private Bitmap CreateBitmapImage(string imageText, string imageUrl)
{
Bitmap myBitmap = new Bitmap(imageUrl);
Graphics g = Graphics.FromImage(myBitmap);
g.DrawString(imageText, new Font("Tahoma", 40), Brushes.White, new PointF(0, 0));
return (objBmpImage);
}
Once you have the Bitmap object in memory you can save it to the disk and call it's location from the web.
Bitmap bmp = CreateBitmapImage("my picture", #"C:\myBasePic.bmp");
bmp.Save(#"C:\bla.png", System.Drawing.Imaging.ImageFormat.png);
It would be good if you can specify why such requirement is there. One of such scenario that I had encountered was need of image buttons with different text (round corners with shaded background) - this can easily be achieved using CSS. If you really want to combine an image and text together then you can do that at server side (using say System.Drawing types) and serve the combined image over a handler/web-service.

Categories