调整JPEG图像大小而不会降低质量

时间:2013-04-12 03:57:07

标签: asp.net c#-4.0

我有这个代码将图像大小调整为300x300。但在调整大小后,图像仍然会失去其质量。我已尝试在其他网站上发布的其他解决方案与此代码具有相同的代码实现,但它对我不起作用。请帮帮我。

Stream ream = FileUploadPic.FileContent;
            System.Drawing.Image i = new Bitmap(ream);

            Bitmap Orig_Photo = new Bitmap(i);
            Bitmap resize_Photo = new Bitmap(300,300);
            Graphics thumbnailGraph = Graphics.FromImage(resize_Photo);
            thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
            thumbnailGraph.SmoothingMode = SmoothingMode.AntiAlias;
            thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
            thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;

            var imageRectangle = new Rectangle(0, 0, 300, 300);
            thumbnailGraph.DrawImage(i, imageRectangle);
            System.Drawing.Image ii = new Bitmap(resize_Photo);

2 个答案:

答案 0 :(得分:1)

使用此

// Create image object from upload stream  
Dim img As System.Drawing.Bitmap = System.Drawing.Bitmap.FromStream(.InputStream)
Dim imageFormat = img.RawFormat 
Dim thumbSize As New Size thumbSize = NewthumbSize(img.Width, img.Height)  
'Thumb-nail the image to the new size'  
Dim imgOutput As New Bitmap(img, thumbSize.Width,thumbSize.Height)   
Dim myresizer As Graphics  myresizer = Graphics.FromImage(imgOutput)
myresizer.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
myresizer.DrawImage(img, 0, 0, thumbSize.Width, thumbSize.Height)
'Save original image 
imgOutput.Save(Path.Combine(Server.MapPath("../photo/images"),fileName), imageFormat)
string fileName = albumlist.SelectedValue + "_" + currentDate + fileExtension;

我使用了我原来的vb.net代码并使用developer fusion的转换器转换它,希望这对你有用!

根据您的要求修改此内容!

答案 1 :(得分:0)

我曾经使用过这段代码,所以你可以尝试一下这就是c#version

  Bitmap img = new Bitmap(newW, newH);
        using (Graphics g = Graphics.FromImage(img))
        {
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.DrawImage(srcImage, new Rectangle(0, 0, newW, newH));
        }

此处newWnewH是您要重新调整图片大小的新高度和宽度。

您还需要包含using System.Drawing;名称空间。