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;
}
Related
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;
}
Following this question Resize image proportionally with MaxHeight and MaxWidth constraints
I implemented the solution as follows:
public static class ImageResizer
{
public static Image ResizeImage(String origFileLocation, int maxWidth, int maxHeight)
{
Image img = Image.FromFile(origFileLocation);
if (img.Height < maxHeight && img.Width < maxWidth) return img;
using (img)
{
Double xRatio = (double)maxWidth / img.Width;
Double yRatio = (double)maxHeight / img.Height;
Double ratio = Math.Max(xRatio, yRatio);
int nnx = (int)Math.Floor(img.Width * ratio);
int nny = (int)Math.Floor(img.Height * ratio);
Bitmap cpy = new Bitmap(nnx, nny, PixelFormat.Format32bppArgb);
using (Graphics gr = Graphics.FromImage(cpy))
{
gr.Clear(Color.Transparent);
// This is said to give best quality when resizing images
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.DrawImage(img,
new Rectangle(0, 0, nnx, nny),
new Rectangle(0, 0, img.Width, img.Height),
GraphicsUnit.Pixel);
}
return cpy;
}
}
}
And then saving image this way:
resized.Save(resizedFilePath, System.Drawing.Imaging.ImageFormat.Gif);
However, trying it, shrinking image, the result is very grained as you can see in this photo
I would assume that shrinking image should result with no noticeable effects. Any ideas about it?
Yes. As #Ivan Stoev suggested. The problem is not with the resizing, rather in the saving method which appears to compress the image by default for some reason.
I used
resized.Save(resizedFilePath, System.Drawing.Imaging.ImageFormat.Png);
for saving and now everything seems fine. Thank you.
resized.Save(resizedFilePath, System.Drawing.Imaging.ImageFormat.Png);
size large not optimation
This question already has answers here:
Resize image proportionally with MaxHeight and MaxWidth constraints
(3 answers)
Closed 9 years ago.
Am re sizing an image with the following code
using (Image thumbnail = new Bitmap(100, 50))
{
using (Bitmap source = new Bitmap(imageFile))
{
using (Graphics g = Graphics.FromImage(thumbnail))
{
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(source, 0, 0, 100, 50);
}
}
using (MemoryStream ms = new MemoryStream())
{
thumbnail.Save(ms, ImageFormat.Png);
thumbnail.Save(dest, ImageFormat.Png);
}
}
but it is not giving an image of any quality. pixelation is making the image wired.
i have also tried the code
image re size in stack
but am getting a black screen as the result instead of jpg am using png is the only difference.
any suggestion for improving the image quality. i have to re size the transparent image to a size 100by50.
Thanks in advance.
Try this, Assuming you can use it
public static Image Resize(Image originalImage, int w, int h)
{
//Original Image attributes
int originalWidth = originalImage.Width;
int originalHeight = originalImage.Height;
// Figure out the ratio
double ratioX = (double)w / (double)originalWidth;
double ratioY = (double)h / (double)originalHeight;
// use whichever multiplier is smaller
double ratio = ratioX < ratioY ? ratioX : ratioY;
// now we can get the new height and width
int newHeight = Convert.ToInt32(originalHeight * ratio);
int newWidth = Convert.ToInt32(originalWidth * ratio);
Image thumbnail = new Bitmap(newWidth, newHeight);
Graphics graphic = Graphics.FromImage(thumbnail);
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;
graphic.Clear(Color.Transparent);
graphic.DrawImage(originalImage, 0, 0, newWidth, newHeight);
return thumbnail;
}
Is possible to resize image proportionally in a way independent of the image type (bmp, jpg, png, etc)?
I have this code and know that something is missing (but don't know what):
public bool ResizeImage(string fileName, string imgFileName,
ImageFormat format, int width, int height)
{
try
{
using (Image img = Image.FromFile(fileName))
{
Image thumbNail = new Bitmap(width, height, img.PixelFormat);
Graphics g = Graphics.FromImage(thumbNail);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle rect = new Rectangle(0, 0, width, height);
g.DrawImage(img, rect);
thumbNail.Save(imgFileName, format);
}
return true;
}
catch (Exception)
{
return false;
}
}
If not possible, how can I resize proportional a jpeg image?
I know that using this method, but don't know where to put this (!).
First and foremost, you're not grabbing the CURRENT height and width of the image. In order to resize proportionately you'll need to grab the current height/width of the image and resize based on that.
From there, find the greatest attribute and resize proportionately based on that.
For instance, let's say the current image is 800 x 600 and you wanna resize proportionately within a 400 x 400 space. Grab the greatest proportion (800) and find it's ratio to the new size. 800 -> 400 = .5 Now take that ratio and multiply by the second dimension (600 * .5 = 300).
Your new size is 400 x 300.
Here's a PHP example (sorry....you'll get it though)
$thumb_width = 400;
$thumb_height = 400;
$orig_w=imagesx($src_img);
$orig_h=imagesy($src_img);
if ($orig_w>$orig_h){//find the greater proportion
$ratio=$thumb_width/$orig_w;
$thumb_height=$orig_h*$ratio;
}else{
$ratio=$thumb_height/$orig_h;
$thumb_width=$orig_w*$ratio;
}
I think your code is fine, but taking in the width and the height as parameters is where you're going wrong in my opinion. Why should the caller of this method have to decide how big they want the width and the height? I would suggest changing it to a percentage:
public bool ResizeImage(string fileName, string imgFileName,
ImageFormat format, int percent)
{
try
{
using (Image img = Image.FromFile(fileName))
{
int width = img.Width * (percent * .01);
int height = img.Height * (percent * .01);
Image thumbNail = new Bitmap(width, height, img.PixelFormat);
Graphics g = Graphics.FromImage(thumbNail);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle rect = new Rectangle(0, 0, width, height);
g.DrawImage(img, rect);
thumbNail.Save(imgFileName, format);
}
return true;
}
catch (Exception)
{
return false;
}
}