I need to resize an image, but the image quality cannot be affected by this.
我需要调整图像大小,但图像质量不会受此影响。
11 个解决方案
#1
204
As rcar says, you can't without losing some quality, the best you can do in c# is:
正如rcar所说,你不能没有失去一些品质,你可以用c#做的最好的事情是:
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));
}
#2
32
Unless you're doing vector graphics, there's no way to resize an image without potentially losing some image quality.
除非你正在做矢量图形,否则无法在不丢失图像质量的情况下调整图像大小。
#3
21
private static Image resizeImage(Image imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (Image)b;
}
from here
#4
5
I believe what you're looking to do is "Resize/Resample" your images. Here is a good site that gives instructions and provides a utility class(That I also happen to use):
我相信你要做的是“调整/重新采样”你的图像。这是一个很好的网站,提供说明并提供实用程序类(我也碰巧使用):
http://www.codeproject.com/KB/GDI-plus/imgresizoutperfgdiplus.aspx
#5
3
Unless you resize up, you cannot do this with raster graphics.
除非你调整大小,否则你无法使用光栅图形。
What you can do with good filtering and smoothing is to resize without losing any noticable quality.
您可以通过良好的过滤和平滑来完成调整,而不会丢失任何明显的质量。
You can also alter the DPI metadata of the image (assuming it has some) which will keep exactly the same pixel count, but will alter how image editors think of it in 'real-world' measurements.
您还可以更改图像的DPI元数据(假设它有一些),这将保持完全相同的像素数,但会改变图像编辑器在“真实世界”测量中的想法。
And just to cover all bases, if you really meant just the file size of the image and not the actual image dimensions, I suggest you look at a lossless encoding of the image data. My suggestion for this would be to resave the image as a .png file (I tend to use paint as a free transcoder for images in windows. Load image in paint, save as in the new format)
只是为了覆盖所有基础,如果你真的只是图像的文件大小而不是实际的图像尺寸,我建议你看一下图像数据的无损编码。我的建议是将图像重新保存为.png文件(我倾向于使用paint作为windows中图像的免费转码器。在paint中加载图像,另存为新格式)
#6
3
You can't resize an image without losing some quality, simply because you are reducing the number of pixels.
您不能在不损失某些质量的情况下调整图像大小,这只是因为您减少了像素数。
Don't reduce the size client side, because browsers don't do a good job of resizing images.
不要减小客户端的大小,因为浏览器不能很好地调整图像大小。
What you can do is programatically change the size before you render it, or as a user uploads it.
您可以做的是在呈现之前以编程方式更改大小,或者在用户上载大小时。
Here is an article that explains one way to do this in c#: http://www.codeproject.com/KB/GDI-plus/imageresize.aspx
这篇文章解释了c#中的一种方法:http://www.codeproject.com/KB/GDI-plus/imageresize.aspx
#7
2
See if you like the image resizing quality of this open source ASP.NET module. There's a live demo, so you can mess around with it yourself. It yields results that are (to me) impossible to distinguish from Photoshop output. It also has similar file sizes - MS did a good job on their JPEG encoder.
看看您是否喜欢这个开源ASP.NET模块的图像大小调整质量。有一个现场演示,所以你可以自己搞乱。它产生的结果(对我来说)无法与Photoshop输出区分开来。它也有类似的文件大小 - MS在他们的JPEG编码器上做得很好。
#8
1
There is something out there, context aware resizing, don't know if you will be able to use it, but it's worth looking at, that's for sure
有一些东西,上下文感知大小调整,不知道你是否能够使用它,但它值得一看,这是肯定的
A nice video demo (Enlarging appears towards the middle) http://www.youtube.com/watch?v=vIFCV2spKtg
一个很好的视频演示(放大到中间)http://www.youtube.com/watch?v=vIFCV2spKtg
Here there could be some code. http://www.semanticmetadata.net/2007/08/30/content-aware-image-resizing-gpl-implementation/
这里可能有一些代码。 http://www.semanticmetadata.net/2007/08/30/content-aware-image-resizing-gpl-implementation/
Was that overkill? Maybe there are some easy filters you can apply to an enlarged image to blur the pixels a bit, you could look into that.
那有点矫枉过正吗?也许有一些简单的滤镜可以应用于放大的图像来模糊像素,你可以看一下。
#9
1
Are you resizing larger, or smaller? By a small % or by a larger factor like 2x, 3x? What do you mean by quality for your application? And what type of images - photographs, hard-edged line drawings, or what? Writing your own low-level pixel grinding code or trying to do it as much as possible with existing libraries (.net or whatever)?
您是在调整大小还是更小?通过一小部分或更大的因素,如2x,3x?您的应用质量是什么意思?什么类型的图像 - 照片,硬边线条图或什么?编写自己的低级像素磨削代码或尝试使用现有库(.net或其他)尽可能多地执行此操作?
There is a large body of knowledge on this topic. The key concept is interpolation.
关于这个主题有很多知识。关键概念是插值。
Browsing recommendations:
* http://www.all-in-one.ee/~dersch/interpolator/interpolator.html
* http://www.cambridgeincolour.com/tutorials/image-interpolation.htm
* for C#: https://secure.codeproject.com/KB/GDI-plus/imageprocessing4.aspx?display=PrintAll&fid=3657&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26&select=629945 * this is java-specific but might be educational - http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
浏览建议:* http://www.all-in-one.ee/~dersch/interpolator/interpolator.html * http://www.cambridgeincolour.com/tutorials/image-interpolation.htm * for C#:https: //secure.codeproject.com/KB/GDI-plus/imageprocessing4.aspx?display=PrintAll&fid=3657&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26&select=629945 *这是特定于java的,但可能具有教育意义 - http: //today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
#10
1
Here you can find also add watermark codes in this class :
您还可以在此处添加水印代码:
public class ImageProcessor
{
public Bitmap Resize(Bitmap image, int newWidth, int newHeight, string message)
{
try
{
Bitmap newImage = new Bitmap(newWidth, Calculations(image.Width, image.Height, newWidth));
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(image, new Rectangle(0, 0, newImage.Width, newImage.Height));
var myBrush = new SolidBrush(Color.FromArgb(70, 205, 205, 205));
double diagonal = Math.Sqrt(newImage.Width * newImage.Width + newImage.Height * newImage.Height);
Rectangle containerBox = new Rectangle();
containerBox.X = (int)(diagonal / 10);
float messageLength = (float)(diagonal / message.Length * 1);
containerBox.Y = -(int)(messageLength / 1.6);
Font stringFont = new Font("verdana", messageLength);
StringFormat sf = new StringFormat();
float slope = (float)(Math.Atan2(newImage.Height, newImage.Width) * 180 / Math.PI);
gr.RotateTransform(slope);
gr.DrawString(message, stringFont, myBrush, containerBox, sf);
return newImage;
}
}
catch (Exception exc)
{
throw exc;
}
}
public int Calculations(decimal w1, decimal h1, int newWidth)
{
decimal height = 0;
decimal ratio = 0;
if (newWidth < w1)
{
ratio = w1 / newWidth;
height = h1 / ratio;
return height.To<int>();
}
if (w1 < newWidth)
{
ratio = newWidth / w1;
height = h1 * ratio;
return height.To<int>();
}
return height.To<int>();
}
}
#11
0
Here is a forum thread that provides a C# image resizing code sample. You could use one of the GD library binders to do resampling in C#.
这是一个论坛帖子,它提供了一个C#图像大小调整代码示例。您可以使用其中一个GD库绑定器在C#中进行重新采样。
#1
204
As rcar says, you can't without losing some quality, the best you can do in c# is:
正如rcar所说,你不能没有失去一些品质,你可以用c#做的最好的事情是:
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));
}
#2
32
Unless you're doing vector graphics, there's no way to resize an image without potentially losing some image quality.
除非你正在做矢量图形,否则无法在不丢失图像质量的情况下调整图像大小。
#3
21
private static Image resizeImage(Image imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (Image)b;
}
from here
#4
5
I believe what you're looking to do is "Resize/Resample" your images. Here is a good site that gives instructions and provides a utility class(That I also happen to use):
我相信你要做的是“调整/重新采样”你的图像。这是一个很好的网站,提供说明并提供实用程序类(我也碰巧使用):
http://www.codeproject.com/KB/GDI-plus/imgresizoutperfgdiplus.aspx
#5
3
Unless you resize up, you cannot do this with raster graphics.
除非你调整大小,否则你无法使用光栅图形。
What you can do with good filtering and smoothing is to resize without losing any noticable quality.
您可以通过良好的过滤和平滑来完成调整,而不会丢失任何明显的质量。
You can also alter the DPI metadata of the image (assuming it has some) which will keep exactly the same pixel count, but will alter how image editors think of it in 'real-world' measurements.
您还可以更改图像的DPI元数据(假设它有一些),这将保持完全相同的像素数,但会改变图像编辑器在“真实世界”测量中的想法。
And just to cover all bases, if you really meant just the file size of the image and not the actual image dimensions, I suggest you look at a lossless encoding of the image data. My suggestion for this would be to resave the image as a .png file (I tend to use paint as a free transcoder for images in windows. Load image in paint, save as in the new format)
只是为了覆盖所有基础,如果你真的只是图像的文件大小而不是实际的图像尺寸,我建议你看一下图像数据的无损编码。我的建议是将图像重新保存为.png文件(我倾向于使用paint作为windows中图像的免费转码器。在paint中加载图像,另存为新格式)
#6
3
You can't resize an image without losing some quality, simply because you are reducing the number of pixels.
您不能在不损失某些质量的情况下调整图像大小,这只是因为您减少了像素数。
Don't reduce the size client side, because browsers don't do a good job of resizing images.
不要减小客户端的大小,因为浏览器不能很好地调整图像大小。
What you can do is programatically change the size before you render it, or as a user uploads it.
您可以做的是在呈现之前以编程方式更改大小,或者在用户上载大小时。
Here is an article that explains one way to do this in c#: http://www.codeproject.com/KB/GDI-plus/imageresize.aspx
这篇文章解释了c#中的一种方法:http://www.codeproject.com/KB/GDI-plus/imageresize.aspx
#7
2
See if you like the image resizing quality of this open source ASP.NET module. There's a live demo, so you can mess around with it yourself. It yields results that are (to me) impossible to distinguish from Photoshop output. It also has similar file sizes - MS did a good job on their JPEG encoder.
看看您是否喜欢这个开源ASP.NET模块的图像大小调整质量。有一个现场演示,所以你可以自己搞乱。它产生的结果(对我来说)无法与Photoshop输出区分开来。它也有类似的文件大小 - MS在他们的JPEG编码器上做得很好。
#8
1
There is something out there, context aware resizing, don't know if you will be able to use it, but it's worth looking at, that's for sure
有一些东西,上下文感知大小调整,不知道你是否能够使用它,但它值得一看,这是肯定的
A nice video demo (Enlarging appears towards the middle) http://www.youtube.com/watch?v=vIFCV2spKtg
一个很好的视频演示(放大到中间)http://www.youtube.com/watch?v=vIFCV2spKtg
Here there could be some code. http://www.semanticmetadata.net/2007/08/30/content-aware-image-resizing-gpl-implementation/
这里可能有一些代码。 http://www.semanticmetadata.net/2007/08/30/content-aware-image-resizing-gpl-implementation/
Was that overkill? Maybe there are some easy filters you can apply to an enlarged image to blur the pixels a bit, you could look into that.
那有点矫枉过正吗?也许有一些简单的滤镜可以应用于放大的图像来模糊像素,你可以看一下。
#9
1
Are you resizing larger, or smaller? By a small % or by a larger factor like 2x, 3x? What do you mean by quality for your application? And what type of images - photographs, hard-edged line drawings, or what? Writing your own low-level pixel grinding code or trying to do it as much as possible with existing libraries (.net or whatever)?
您是在调整大小还是更小?通过一小部分或更大的因素,如2x,3x?您的应用质量是什么意思?什么类型的图像 - 照片,硬边线条图或什么?编写自己的低级像素磨削代码或尝试使用现有库(.net或其他)尽可能多地执行此操作?
There is a large body of knowledge on this topic. The key concept is interpolation.
关于这个主题有很多知识。关键概念是插值。
Browsing recommendations:
* http://www.all-in-one.ee/~dersch/interpolator/interpolator.html
* http://www.cambridgeincolour.com/tutorials/image-interpolation.htm
* for C#: https://secure.codeproject.com/KB/GDI-plus/imageprocessing4.aspx?display=PrintAll&fid=3657&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26&select=629945 * this is java-specific but might be educational - http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
浏览建议:* http://www.all-in-one.ee/~dersch/interpolator/interpolator.html * http://www.cambridgeincolour.com/tutorials/image-interpolation.htm * for C#:https: //secure.codeproject.com/KB/GDI-plus/imageprocessing4.aspx?display=PrintAll&fid=3657&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26&select=629945 *这是特定于java的,但可能具有教育意义 - http: //today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
#10
1
Here you can find also add watermark codes in this class :
您还可以在此处添加水印代码:
public class ImageProcessor
{
public Bitmap Resize(Bitmap image, int newWidth, int newHeight, string message)
{
try
{
Bitmap newImage = new Bitmap(newWidth, Calculations(image.Width, image.Height, newWidth));
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(image, new Rectangle(0, 0, newImage.Width, newImage.Height));
var myBrush = new SolidBrush(Color.FromArgb(70, 205, 205, 205));
double diagonal = Math.Sqrt(newImage.Width * newImage.Width + newImage.Height * newImage.Height);
Rectangle containerBox = new Rectangle();
containerBox.X = (int)(diagonal / 10);
float messageLength = (float)(diagonal / message.Length * 1);
containerBox.Y = -(int)(messageLength / 1.6);
Font stringFont = new Font("verdana", messageLength);
StringFormat sf = new StringFormat();
float slope = (float)(Math.Atan2(newImage.Height, newImage.Width) * 180 / Math.PI);
gr.RotateTransform(slope);
gr.DrawString(message, stringFont, myBrush, containerBox, sf);
return newImage;
}
}
catch (Exception exc)
{
throw exc;
}
}
public int Calculations(decimal w1, decimal h1, int newWidth)
{
decimal height = 0;
decimal ratio = 0;
if (newWidth < w1)
{
ratio = w1 / newWidth;
height = h1 / ratio;
return height.To<int>();
}
if (w1 < newWidth)
{
ratio = newWidth / w1;
height = h1 * ratio;
return height.To<int>();
}
return height.To<int>();
}
}
#11
0
Here is a forum thread that provides a C# image resizing code sample. You could use one of the GD library binders to do resampling in C#.
这是一个论坛帖子,它提供了一个C#图像大小调整代码示例。您可以使用其中一个GD库绑定器在C#中进行重新采样。