我的图像没有压缩

时间:2015-05-14 13:47:10

标签: c# asp.net-mvc-5 system.drawing.imaging

从文件系统加载代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context=".MainActivity">

<include
    android:id="@+id/tool_bar"
    layout="@layout/custom_toolbar"
    ></include>

<TextView
    android:layout_below="@+id/tool_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

从浏览器请求加载代码:

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>);

无论我对此图像做什么,当它保存时,它都会保存到非常高的kb。

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

我做错了什么?

1 个答案:

答案 0 :(得分:1)

正如Magnus正确地说的那样,画布上的绘图与文件的大小没有区别......

保存文件部分是全部菜鸟......这应该是:

    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);
}