Strange border after image resize C# - 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!

Related

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.

Resize GIF and save animation?

I'm using this code i wrote to resize my images to fit my PictureBox:
//Creates a new Bitmap as the size of the window
Bitmap bmp = new Bitmap(this.Width, this.Height);
//Creates a new graphics to handle the image that is coming from the stream
Graphics g = Graphics.FromImage((Image)bmp);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//Resizes the image from the stream to fit our windows
g.DrawImage(Properties.Resources.loading, 0, 0, this.Width, this.Height);
this.Image = (Image)bmp;
Works perfect !
The only problam is when im trying to resize a GIF... it resizes but i lose the animation...
Any fix for that?
You should simply set the PictureBox's SizeMode to StretchImage to make the PictureBox stretch the image for you.

.NET - Border around resized imaged

I'm trying to resize an image in .NET, but get a faint black border around the resized image. I found a post - http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/cf765094-c8c1-4991-a1f3-cecdbd07ee15/ which from someone who said making the destination rectangle larger than the canvas worked, but this doesn't work for me. It gets riid of the top and left borders, but the right and bottom are still there, and are a full 1px thick black.
Am I missing something? My code is below.
Image image = ... // this is a valid image loaded from the source
Rectangle srcRectangle = new Rectangle(0,0,width, height);
Size croppedFullSize = new Size(width+3,height+3);
Rectangle destRect = new Rectangle(new Point(-1,-1), croppedFullSize);
using(Bitmap newImage = new Bitmap(croppedFullSize.Width, croppedFullSize.Height, format))
using(Graphics Canvas = Graphics.FromImage(newImage)) {
Canvas.SmoothingMode = SmoothingMode.AntiAlias;
Canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
Canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
Canvas.FillRectangle(Brushes.Transparent, destRect);
Canvas.DrawImage(image, destRect, srcRectangle, GraphicsUnit.Pixel);
newImage.Save(filename, image.RawFormat);
}
Simply provide the DrawImage method with an ImageAttributes instance which has WrapMode set to TileFlipXY. This will prevent the edge from blending against the background color.
For sample code that doesn't leak memory like the other answers here, see this gist
Try it like this, i think i've never got a black border...
If you want to use System.Drawing libraries:
using (var sourceBmp = new Bitmap(sourcePath))
{
decimal aspect = (decimal)sourceBmp.Width / (decimal)sourceBmp.Height;
int newHeight = (int)(newWidth / aspect);
using (var destinationBmp = new Bitmap(newWidth, newHeight))
{
using (var destinationGfx = Graphics.FromImage(destinationBmp))
{
destinationGfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
destinationGfx.DrawImage(sourceBmp, new Rectangle(0, 0, destinationBmp.Width, destinationBmp.Height));
destinationBmp.Save(destinationPath, ImageFormat.Jpeg);
}
}
}
or you can do the same with wpf, like this:
using (var output = new FileStream(outputPath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None))
{
var imageDecoder = BitmapDecoder.Create(inputStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
var imageFrame = imageDecoder.Frames[0];
decimal aspect = (decimal)imageFrame.Width / (decimal)imageFrame.Height;
var height = (int)(newWidth / aspect);
var imageResized = new TransformedBitmap(imageFrame,new ScaleTransform(
newWidth / imageFrame.Width * Dpi / imageFrame.DpiX,
height / imageFrame.Height * Dpi / imageFrame.DpiY, 0, 0));
var targetFrame = BitmapFrame.Create(imageResized);
var targetEncoder = new JpegBitmapEncoder();
targetEncoder.Frames.Add(targetFrame);
targetEncoder.QualityLevel = 80;
targetEncoder.Save(output);
}
I recommend the WPF way. The compression & quality seems better...
For me it was a bad Bitmap parameter. Instead of this:
new Bitmap(width, height, PixelFormat.Format32bppPArgb);
Just remove the PixelFormat to this:
new Bitmap(width, height);
And everything was ok then.
With the PixelFormat I had black border on the top and left border. Then I tried g.PixelOffsetMode = PixelOffsetMode.HighQuality; which seemed fine at first. But then I noticed light gray borders around the whole image.

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