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

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.

Related

Write text on image C# with accurate location

The thing is that I’m trying to write txt on image using setting from an editable C# label through my custom edit panel (txt, font name, font size, font style, location), then after I finish the edit I press add the text to the image!
It always gets written in wrong location and wrong font size
The picture is loaded in picture box and the size mode is stretched.
The interface preview
Code:
Bitmap bmp = (Bitmap)pictureBox1.Image;
RectangleF rectf = new RectangleF(NamePreviewPanel.Location.X , NamePreviewPanel.Location.Y, bmp.Width, bmp.Height);
Graphics g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
StringFormat format = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
// add the color
int gW = (int)(TestFontLable.Text.Length * TestFontLable.Font.Size);
gW = gW == 0 ? 10 : gW;
LinearGradientBrush LGBrush = new LinearGradientBrush(rectf, TestFontLable.ForeColor, TestFontLable.ForeColor, LinearGradientMode.Vertical);
g.DrawString(TestFontLable.Text, TestFontLable.Font, LGBrush, rectf, format);
pictureBox1.Image = bmp;

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

c# write text on bitmap

I have following problem. I want to make some graphics in c# windows form.
I want to read bitmap to my program and after it write some text on this bitmap. In the end I want this picture load to pictureBox. And it's my question. How can I do it?
example, how must it work:
Bitmap a = new Bitmap(#"path\picture.bmp");
a.makeTransparent();
// ? a.writeText("some text", positionX, positionY);
pictuteBox1.Image = a;
Is it possible do to?
Bitmap bmp = new Bitmap("filename.bmp");
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();
image.Image=bmp;
Very old question, but just had to build this for an app today and found the settings shown in other answers do not result in a clean image (possibly as new options were added in later .Net versions).
Assuming you want the text in the centre of the bitmap, you can do this:
// Load the original image
Bitmap bmp = new Bitmap("filename.bmp");
// Create a rectangle for the entire bitmap
RectangleF rectf = new RectangleF(0, 0, bmp.Width, bmp.Height);
// Create graphic object that will draw onto the bitmap
Graphics g = Graphics.FromImage(bmp);
// ------------------------------------------
// Ensure the best possible quality rendering
// ------------------------------------------
// The smoothing mode specifies whether lines, curves, and the edges of filled areas use smoothing (also called antialiasing).
// One exception is that path gradient brushes do not obey the smoothing mode.
// Areas filled using a PathGradientBrush are rendered the same way (aliased) regardless of the SmoothingMode property.
g.SmoothingMode = SmoothingMode.AntiAlias;
// The interpolation mode determines how intermediate values between two endpoints are calculated.
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Use this property to specify either higher quality, slower rendering, or lower quality, faster rendering of the contents of this Graphics object.
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
// This one is important
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
// Create string formatting options (used for alignment)
StringFormat format = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
// Draw the text onto the image
g.DrawString("yourText", new Font("Tahoma",8), Brushes.Black, rectf, format);
// Flush all graphics changes to the bitmap
g.Flush();
// Now save or use the bitmap
image.Image = bmp;
References
https://msdn.microsoft.com/en-us/library/system.drawing.graphics.smoothingmode(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.interpolationmode(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.drawing.graphics.pixeloffsetmode(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.drawing.graphics.textrenderinghint(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.drawing.stringformat(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/21kdfbzs(v=vs.110).aspx
You need to use the Graphics class in order to write on the bitmap.
Specifically, one of the DrawString methods.
Bitmap a = new Bitmap(#"path\picture.bmp");
using(Graphics g = Graphics.FromImage(a))
{
g.DrawString(....); // requires font, brush etc
}
pictuteBox1.Image = a;
var bmp = new Bitmap(#"path\picture.bmp");
using( Graphics g = Graphics.FromImage( bmp ) )
{
g.DrawString( ... );
}
picturebox1.Image = bmp;
If you want wrap your text, then you should draw your text in a rectangle:
RectangleF rectF1 = new RectangleF(30, 10, 100, 122);
e.Graphics.DrawString(text1, font1, Brushes.Blue, rectF1);
See: https://msdn.microsoft.com/en-us/library/baw6k39s(v=vs.110).aspx

Resizing a monochromatic image in 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;

Categories