Saving a Drawn Image as MonoChrome - c#

Hi everyone i have a image that i draw using graphics.drawline
Bitmap Signature = new Bitmap(x, y);
Graphics g;
g = Graphics.FromImage(Signature);
//MessageBox.Show(cord.Length.ToString());
Pen mypen = new Pen(Brushes.Black);
mypen.Width = 2;
mypen.EndCap = System.Drawing.Drawing2D.LineCap.Square;
mypen.StartCap = System.Drawing.Drawing2D.LineCap.Square;
anyways
Signature.Save(filename);
this works great i am trying to make this image monochrome i have tried many different solutions such as this
Save a 32-bit Bitmap as 1-bit .bmp file in C#
also as soon as i reference the imagetype.bmp it turns black any ideal or suggestions on this, i ran the above link like this
Bitmap converted = BitmapTo1Bpp(Signature);
converted.Save(filename);
but the resulting picture is always pure black can someone please help me save this as a monochrome image

After more searching i found that
g = Graphics.FromImage(Signature);
g.Clear(Color.White);
Adding a White Background to the Graphics Drawing Fixed The Problem

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

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

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

C# resize, resave .tif as .jpg gives a blue tint to the new image

I'm building a batch processor to save a directory of .tif images as .jpgs. The processing is working fine. However, the rendered jpgs have a blue-ish tint to them. They aren't "blue", as much as they have a cooler hue, a blue hue. The originals are much brighter and warmer in color. This is how I am creating the resized jpeg:
Bitmap bitmap = new Bitmap(image.Image, size);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.None;
graphics.DrawImage(image.Image, 0, 0, size.Width, size.Height);
// Get EncoderInfo("image/jpeg") gets the jpeg Codec by mime type
bitmap.Save(path, GetEncoderInfo("image/jpeg"), EncoderParameters);
The original tif images are 7MB is size - large in comparison to the rendered jpegs. Perhaps that has something to do with it. Not sure.
I've come up empty on the Googles. Does anyone have any experience with this or any advice on what to try next? Thanks!
It could be that the original files have a color profile. In that case, you need to copy that information into the new file as well. I don't know how to do that with .NET's imaging classes.
I would first suggest a test:
Create a plain image with a single colour.
Convert it to jpeg.
Then check in a photo package if the hue has actually changed or if it is your perception.
From the MSDN documentation, it looks like you can be doing this somewhat simpler (assuming image.Image is an actual System.Drawing.Image instance):
image.Image.Save(path, System.Drawing.ImageFormat.Jpeg)
That might help - it looks like you're taking your TIF, converting to a bitmap, and only then converting to a JPG.

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