0

从文件系统加载代码:

System.Drawing.Image image = System.Drawing.Image.FromFile(<location of original image>););    

从浏览器请求加载代码:

var memoryStream = new MemoryStream();
using (memoryStream)
{
    System.Web.HttpContext.Current.Request.Files[upload].InputStream.CopyTo(memoryStream);
    memoryStream.ToArray();
}

byte[] bytes = memoryStream.GetBuffer();    

// Get the image from the server
System.Drawing.Image image = new System.Drawing.Bitmap( System.Web.HttpContext.Current.Request.Files[upload].InputStream );

调整图像大小调用:

System.Drawing.Image image = this.ResizeImage(
    image, 
    originalImagePath, 
    ImageSizeType.Original, 
    null, 
    null)

保存图像调用:

image.Save(<location to save>);

不压缩图像的代码:

private System.Drawing.Image ResizeImage(System.Drawing.Image image, string filePath, string sizeType, int? _width, int? height )
{
    ...
    System.Drawing.Bitmap b = new System.Drawing.Bitmap(width, resizeHeight);
    b.SetResolution(72, 72);
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage((System.Drawing.Image)b);
    g.CompositingQuality = CompositingQuality.HighSpeed;
    //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.InterpolationMode = InterpolationMode.Low;
    g.SmoothingMode = SmoothingMode.HighSpeed;
    g.DrawImage(image, 0, 0, width, resizeHeight);
    g.Dispose();
    return (System.Drawing.Image)b;
}

无论我对这张图片做什么,当它保存时,它会以非常高的 kb 保存。

例如... 1024 x 768 @ 300kb 的 jpg 变为 600 x 400 @ 800kb

我究竟做错了什么?

4

1 回答 1

1

正如马格努斯正确地说的那样,在画布上绘图对文件的大小没有影响......

这是所有菜鸟的保存文件部分......这应该是:

    private ImageCodecInfo GetEncoderInfo(string mimeType)
    {
        // Get image codecs for all image formats
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

        // Find the correct image codec
        for (int i = 0; i < codecs.Length; i++)
            if (codecs[i].MimeType == mimeType)
                return codecs[i];
        return null;
    }


...
if( mimeType.ToLower() == "image/jpeg")
{
    ImageCodecInfo jpgEncoder = this.GetEncoderInfo("image/jpeg")

    System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;

    EncoderParameters myEncoderParameters = new EncoderParameters(1);
    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 80L);
    myEncoderParameters.Param[0] = myEncoderParameter;

    image.Save(systemFilePath, jpgEncoder, myEncoderParameters);
}
else
{
    image.Save(systemFilePath);
}
于 2015-05-14T14:37:18.190 回答