C# 在不损失任何质量的情况下调整图像大小
如何在图像质量不受影响的情况下调整图像大小?除非您正在使用矢量图形,否则无法调整图像大小而不降低图像质量。如前所述,在不降低图像质量的情况下,您无法做到这一点,在c语言中最好的做法是:C# 在不损失任何质量的情况下调整图像大小,c#,image,image-scaling,C#,Image,Image Scaling,如何在图像质量不受影响的情况下调整图像大小?除非您正在使用矢量图形,否则无法调整图像大小而不降低图像质量。如前所述,在不降低图像质量的情况下,您无法做到这一点,在c语言中最好的做法是: 您无法调整图像的大小而不损失一些质量,因为您正在减少像素数 不要减少客户端的大小,因为浏览器不能很好地调整图像的大小 您可以做的是在渲染之前或在用户上传之前通过编程更改大小 这里有一篇文章解释了在c#中实现这一点的一种方法: 除非调整大小,否则无法使用光栅图形执行此操作 通过良好的过滤和平滑处理,您可以调整大小,
您无法调整图像的大小而不损失一些质量,因为您正在减少像素数
不要减少客户端的大小,因为浏览器不能很好地调整图像的大小 您可以做的是在渲染之前或在用户上传之前通过编程更改大小 这里有一篇文章解释了在c#中实现这一点的一种方法:
除非调整大小,否则无法使用光栅图形执行此操作 通过良好的过滤和平滑处理,您可以调整大小,而不会丢失任何明显的质量 您还可以更改图像的DPI元数据(假设它有一些),这将保持完全相同的像素计数,但将改变图像编辑器在“真实世界”测量中对它的看法
为了涵盖所有基础,如果你真的只是指图像的文件大小,而不是实际的图像尺寸,我建议你看看图像数据的无损编码。我的建议是将图像重新保存为.png文件(我倾向于使用paint作为windows中图像的免费转码器。在paint中加载图像,另存为新格式)这里有一些东西,可以根据上下文调整大小,不知道您是否能够使用它,但确实值得一看 一个不错的视频演示(放大显示在中间) 这里可能有一些代码。
那是不是太过分了?也许有一些简单的过滤器可以应用到放大的图像上,使像素模糊一点,你可以看看。你是放大还是缩小?小的%还是更大的因子,比如2x,3x?你所说的申请质量是什么意思?什么类型的图像-照片,硬边线条图,还是什么?编写您自己的低级像素研磨代码,或者尝试使用现有库(.net或其他什么)尽可能多地执行此操作 关于这个话题有大量的知识。关键概念是插值 浏览建议:
*
*
*对于C#:
*这是特定于java的,但可能具有教育意义-下面是一个提供C#图像大小调整代码示例的示例。您可以使用其中一个活页夹在C#中进行重采样。看看您是否喜欢现场演示,这样您就可以自己动手了。它产生的结果(对我来说)无法与Photoshop输出区分。它也有相似的文件大小-MS在JPEG编码器上做得很好。我相信你要做的是“调整图像大小/重新采样”。这是一个很好的网站,提供了说明和实用程序类(我也碰巧使用过):
专用静态图像大小图像(图像imgToResize,大小)
{
int sourceWidth=imgToResize.Width;
int sourceHeight=imgToResize.Height;
浮动百分比=0;
浮动nPercentW=0;
浮点数nPercentH=0;
nPercentW=((float)size.Width/(float)sourceWidth);
nPercentH=((浮点)size.Height/(浮点)sourceHeight);
如果(nPercentH
在中,您还可以在该类中找到添加水印代码:
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>();
}
}
公共类图像处理器
{
公共位图调整大小(位图图像、int newWidth、int newHeight、字符串消息)
{
尝试
{
位图newImage=新位图(newWidth,计算(image.Width,image.Height,newWidth));
使用(Graphics gr=Graphics.FromImage(newImage))
{
gr.SmoothingMode=SmoothingMode.AntiAlias;
gr.InterpolationMode=InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode=PixelOffsetMode.HighQuality;
gr.DrawImage(图像,新矩形(0,0,newImage.Width,newImage.Height));
var myBrush=新的SolidBrush(颜色:FromArgb(70205205205));
双对角线=Math.Sqrt(newImage.Width*newImage.Width+newImage.Height*newImage.Height);
矩形容器盒=新矩形();
containerBox.X=(int)(对角线/10);
float messageLength=(float)(对角线/消息长度*1);
containerBox.Y=-(int)(messageLength/1.6);
Font stringFont=新字体(“verdana”,messageLength);
StringFormat sf=新的StringFormat();
浮点斜率=(浮点)(数学Atan2(newImage.Height,newImage.Width)*180/Math.PI);
gr.旋转变换(坡度);
gr.DrawString(消息、stringFont、myBrush、containerBox、sf);
返回新图像;
}
}
捕获(异常exc)
{
抛出exc;
}
}
公共整数计算(十进制w1、十进制h1、整数新宽度)
{
十进制高度=0;
十进制比率=0;
如果(新宽度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;
}
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>();
}
}