Resizing a monochromatic image in C# - c#

I have the following code to resize a monochromatic image (hence pixel value is 0[black] or 255[white]) with the following code
Bitmap ResizedCharImage = new Bitmap(newwidth, newheight);
using (Graphics g = Graphics.FromImage((Image)ResizedCharImage))
{
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(CharBitmap, new Rectangle(0, 0, newwidth, newheight),
new Rectangle(0, 0, CharBitmap.Width, CharBitmap.Height), GraphicsUnit.Pixel);
}
The problem that I am having is that after resizing (i am enlarging the image) some of the pixel values become 254, 253, 1, 2 etc. (and so are not monochromatic.) I need that this do not occur. Is this possible, maybe by changing one of the Graphins properties?

Use SmoothingMode.None

apparently problem solved by setting InterpolationMode to
InterpolationMode.NearestNeighbor;

Related

C# - picturebox, graphics.DrawImage and picturebox.Image = null, why?

I'm trying to crop and resize an image in PictureBox1. My code:
//original image for eventually undo
undoImage = pictureBox1.BackgroundImage.Clone() as Image;
Bitmap sourceBitmap = new Bitmap(pictureBox1.BackgroundImage, pictureBox1.Width, pictureBox1.Height);
Graphics g = pictureBox2.CreateGraphics();
g.DrawImage(sourceBitmap, new Rectangle(0, 0, pictureBox2.Width, pictureBox2.Height), rectCropArea, GraphicsUnit.Pixel);
sourceBitmap.Dispose();
And it working properly on two PictureBoxes.
But PictureBox2.Image,PictureBox2.BackgroundImage (and any other including ErrorImage...) = null.
I tried PictureBox.DrawToBitmap, and other, like g.GetHdc() found on google, but unsuccessful.
My question:
How do I properly copy the edited image from PictureBox2 to PictureBox1?
Trivial solution:
undoImage = pictureBox1.BackgroundImage.Clone() as Image;
Bitmap sourceBitmap = new Bitmap(pictureBox1.BackgroundImage, pictureBox1.Width, pictureBox1.Height);
using (Graphics g = Graphics.FromImage(sourceBitmap))
{
g.DrawImage(sourceBitmap, new Rectangle(0, 0, pictureBox2.Width,pictureBox2.Height), rectCropArea, GraphicsUnit.Pixel);
}
pictureBox1.BackgroundImage = sourceBitmap;

Strange border after image resize C#

I'm trying to resize an image wihout loosing quality. I use code from here
Bitmap newImage = new Bitmap(newWidth, newHeight);
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
}
But I got strange problem: a 1px border around the image.
Original image:
Resized:
Not easy to see it, but when you zoom image you can note it, especially in the top-left corner.
How to get rid of it? What is the problem?
Thx!

Save changed image in c# using graphic and bitmap

I read an image and print some text on it as you can see .
Bitmap bmp = new Bitmap(#"d:\a.jpg");
RectangleF rectf = new RectangleF(70, 90, 90, 50);
Graphics g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawString("yourText", new Font("Tahoma", 8), Brushes.Black, rectf);
g.Flush();
How can i save my image and these changes in another location of my computer ?
Dispose Graphics object
g.Dispose();
And save Bitmap
bmp.Save(fileName);
Here you have all Bitmap methods include different versions of Save

why the created bmp image hasn't got high quality?

I have searched a lot and used almost all preferred methods but i have not got the result of having a high quality bitmap image: the below is the code i have used:
ean13.Scale = 0.8f;
ean13.Scale = (float)Convert.ToDecimal(cboScale.Items[cboScale.SelectedIndex]);// it includes the sizes
Bitmap bmp = ean13.CreateBitmap();
Graphics g = Graphics.FromImage(bmp);
g.DrawString(tcc.Text, this.Font, Brushes.Black, 120, 80);// title
g.DrawString(bcc.Text, this.Font, Brushes.Black, 120, 1); //the price
g.SmoothingMode = SmoothingMode.AntiAlias;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
g.Flush();
this.picbox.Image = bmp;
added the picture:
added the expected picture
You should configure the graphics quality before drawing the text. Currently all these changes have no effect on the bitmap.

Overlay bitmap on another bitmap

On a single bitmap I need to display graphs and text values. So what I did is create a bitmap with points and creating a another bitmap with the text and place on the large bitmap.
I tried using the brush to write the text, but I am not able to see the underlying graphics even though trasparency is set.
Instead I thought to set the transparency for the text bitmap, but the bitmap which I have created are 24 bit rgb. So can we set the transparency for the 24 bit map.
Bitmap textBitmap = null;
textBitmap = new Bitmap(10, 10, PixelFormat.Format24bppRgb);
using (Graphics memoryGrahics =
Graphics.FromImage(textBitmap))
{
memoryGrahics.FillRectangle(Brushes.Black, new Rectangle(0, 0, 100, 100));
memoryGrahics.DrawString(result, f, Brushes.White, x, y);
}
//placing the text bitmap on the graphbitmap
using (Graphics g = Graphics.FromImage(GraphBitmap))
{
g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
textBitmap.MakeTransparent();
g.DrawImage(textBitmap, 0, 0);
return GraphBitmap;
}
well.. it seems like you are using 2 different Graphical objects... although 1 Graphics objects with 1 bitmap can handle multiple layouts of custom drawings, like so:
int width = 800, height = 600;
var bit = new Bitmap(width, height);
var g = Graphics.FromImage(bit);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
var area = new Rectangle(0, 0, width, height);
g.FillRectangle(new LinearGradientBrush(area, Color.PaleGoldenrod, Color.OrangeRed, 45), area);
g.DrawImage(Image.FromFile(#"your image"), new Point(10, 10));
g.DrawString("sample", new System.Drawing.Font("Tahoma", 56), new SolidBrush(Color.Black), new PointF(50, 50));
pictureBox1.Image = bit;
note that g.DrawImage method can be used to load other bitmaps as well

Categories