I am trying to resize image to 500x500 px. Using below code:
public static Bitmap ResizeImage(System.Drawing.Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
But this just resizes horizontally and not doing cropping! Someone suggested me to crop the image to a centered 1:1 ratio and then downscale it.
I also wish to increase size of image to 500x500 if it is less than it.
How can I achieve it?
Instead of
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
Use
Rectangle cropRect = new Rectangle(0,0,500,500);
graphics.DrawImage(image, destRect, cropRect, GraphicsUnit.Pixel);
and you can also read more about DrawImageUnscaledAndClipped for cropping purpose.
Related
I have a form without border and need show only a image in the corner of window, the picture dimentions are 9567 x 18012 and size is 62 MB, i use a PictureBox in a form when resize the quality loss, why?, i a need a thirdparty library for show a image to big in dimensions and size?
The left image is a PictureBox with my image and the right is the original image with te information
Use the following code to reduce the image size.
public void SetPictureBoxImage(string path)
{
Image myImage = Image.FromFile(path, true);
int width = 500;
double ratio = (double)myImage.Width / (double)myImage.Height;
int height = Convert.ToInt32(width / ratio);
if (height > width)
{
ratio = (double)myImage.Height / (double)myImage.Width;
height = width;
width = Convert.ToInt32(height / ratio);
}
pictureBox1.Image = ResizeImage(myImage, width, height);
}
Resize Method
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
}
}
return destImage;
}
I am trying to stretch an image to fit new dimensions, but I am not being able to figure out how to make the image to fill the new dimensions, it only creates a larger image size but it keeps the image untouched, I want the image to fill width and height specified.
private Bitmap resizeImage(Image image, int width, int height, float HorizontalResolution, float VerticalResolution)
{
Rectangle destRect = new Rectangle(0, 0, width, height);
Bitmap destImage = new Bitmap(width, height);
destImage.SetResolution(HorizontalResolution, VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
Since you want to stretch the entire source image to a specified size, just use the simple five-argument overload that doesn't accept source coordinates:
DrawImage(Image, Int32, Int32, Int32, Int32)
Like so:
graphics.DrawImage(image, 0, 0, width, height);
Although none of the answers worked for me, I appreciate all the help and suggestions on which I could get to a solution pretty fast, here is the piece of code that worked for me:
private Bitmap resizeImage(Image image, int width, int height, float HorizontalResolution, float VerticalResolution)
{
Rectangle destRect = new Rectangle(0, 0, width, height);
Bitmap destImage = new Bitmap(width, height);
destImage.SetResolution(HorizontalResolution, VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
//drawImage must be set this way to get the desired outcome
graphics.DrawImage(imageBox.Image, new Rectangle(0, 0, image.Width, image.Height), destRect, GraphicsUnit.Pixel);
}
return destImage;
}
Basically I removed the wrapMode code and changed my drawImage method.
You need to draw your image with the newer width and height. It will look like this:
graphics.DrawImage(image, destRect, 0, 0, destRect.Width, destRect.Height, GraphicsUnit.Pixel, wrapMode);
I want resize an Image for fit a size (width and height) without stretch the original image.
My code, resize the image to the specified size but stretch the original image.
this is the code:
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.Tile);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
So you want to Scale with respect to Aspect Ratio.
What we should do is to get a factor and resize both width and height by that factor.
And we need not to go above desired width and height (that's why we use Math.Min on both width and height ratios).
double oldWidth = image.Width;
double oldHeight = image.Height;
var widthRatio = width / oldWidth;
var heightRatio = height / oldHeight;
var factor = Math.Min(widthRatio, heightRatio);
and here we use it like this:
var destRect = new Rectangle(0, 0, (int)(factor * oldWidth), (int)(factor * oldHeight));
I would like to know if its possible in C# to resize image by given percentage proportionally.
For example I have image from which I want to create thumbnail, and I want to resize to 25% of the size, is it possible to put the percentage and apply that within specific method?
I use this method to resize images in an application i made. It uses 'System.Drawing.2D'
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
My problem is that I need to draw text on a monochrome bitmap. The resulting bitmap has to be printed on a thermal POS printer, so the bitmap has to be 1bpp.
I'm not good in graphics, so I've tried to find some samples.
Here's what I've tried:
Bitmap bmp = new Bitmap(300, 300, PixelFormat.Format1bppIndexed);
using (Graphics g = Graphics.FromImage(bmp))
{
Font font = new Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Point);
g.Clear(Color.White);
g.DrawString(text, font, Brushes.Black, 0, 0);
}
bmp.Save(#"c:\x\x.bmp", ImageFormat.Bmp);
the Save at the end was just to check the result.
With this code, I get the following exception: A Graphics object cannot be created from an image that has an indexed pixel format.
Is there ANY way to draw text to a monochrome memory bitmap?
Just for info: I need this because my stupid POS Printer draws a 0 exactly the same way as a O, so they're impossible to distinguish...
Try this:
Bitmap bmp = new Bitmap(300, 300);
using (Graphics g = Graphics.FromImage(bmp))
{
Font font = new Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Point);
g.Clear(Color.White);
g.DrawString("Hello", font, Brushes.Black, 0, 0);
}
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
Bitmap newBitmap = new Bitmap(300, 300, bmpData.Stride, System.Drawing.Imaging.PixelFormat.Format1bppIndexed, bmpData.Scan0);
newBitmap.Save(#"c:\x\x.bmp");
Here is a link that could help:
http://msdn.microsoft.com/en-us/library/zy1a2d14.aspx