C# 调整大小后的低质量图像
您好,我正在使用此功能调整图像大小/上载图像。我可以进行任何快速调整,以便在不改变全部功能的情况下提高调整后的图像质量吗C# 调整大小后的低质量图像,c#,image,resize,image-scaling,C#,Image,Resize,Image Scaling,您好,我正在使用此功能调整图像大小/上载图像。我可以进行任何快速调整,以便在不改变全部功能的情况下提高调整后的图像质量吗 FileUpload FileUpload1 =(FileUpload)ListView1.InsertItem.FindControl("FileUpload1"); string virtualFolder = "~/albume/"; string physicalFolder = Server.Ma
FileUpload FileUpload1 =(FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
string virtualFolder = "~/albume/";
string physicalFolder = Server.MapPath(virtualFolder);
string fileName = Guid.NewGuid().ToString();
string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));
//test resize
System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/albume/") + fileName + extension);
int srcWidth = img.Width;
int srcHeight = img.Height;
int thumbHeight = (int)((800.0 / srcWidth) * srcHeight);
System.Drawing.Image thumb = img.GetThumbnailImage(800, thumbHeight, null, IntPtr.Zero);
img.Dispose();
FileUpload1.Dispose();
thumb.Save(Server.MapPath("~/albume/") + fileName + extension, System.Drawing.Imaging.ImageFormat.Jpeg);
//end resize
myAlbum.poza = fileName + extension;
使用以下代码段:
Bitmap newImage = new Bitmap(newWidth, newHeight);
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
}
从这个问题/答案中盗取:查看Kris的超级投票答案并进行投票:)在我的网站上,我使用以下代码在用户图像发送到页面之前调整其大小 您可以使用类似的方法—尝试研究各种插值模式
using (Graphics graphics = Graphics.FromImage(target))
{
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
graphics.Clear(ColorTranslator.FromHtml("#F4F6F5"));
graphics.DrawImage(bmp, 0, 0, 145, 145);
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
target.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
memoryStream.WriteTo(context.Response.OutputStream);
}
}
最好不要使用“GetThumbnailImage”,这会降低质量。据
通过如下方式重新绘制图像,调整图像大小:
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;
}
private static Image resizeImage(图像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
代替此代码:
thumb.Save(Server.MapPath("~/albume/") + fileName + extension, System.Drawing.Imaging.ImageFormat.Jpeg);
使用这个:
System.Drawing.Imaging.ImageCodecInfo[] info = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.EncoderParameters param = new System.Drawing.Imaging.EncoderParameters(1);
param.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
thumb.Save(Server.MapPath("~/albume/") + fileName + extension, info[1], param);
您的图像是否包含缩略图?如果是这样,这可能适用:“如果您从具有嵌入缩略图的图像请求一个大的缩略图图像(例如,300 x 300),缩略图图像的质量可能会明显下降。通过调用DrawImage方法缩放主图像(而不是缩放嵌入的缩略图)可能会更好。”从。可能重复的正确图像大小可能重复。支持jpeg的正确实现需要5页代码。使用GIF支持的正确实现需要80页代码。我建议。谢谢。我正在尝试实现您推荐的功能,但我得到System.Runtime.InteropServices.ExternalException:GDI+中发生一般错误。。在这方面我是C的新手#