0

Loading code from filesystem:

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

Loading code from browser request:

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

Resize image call:

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

Save image call:

image.Save(<location to save>);

The code that doesn't compress the image:

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

No matter what I do to this image, when it saves, it saves at a really high kb.

For example... a jpg of 1024 x 768 @ 300kb becomes 600 x 400 @ 800kb

What am I doing wrong?

Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
  • 2
    What are the original and resized file formats? You do not show the load / save code. – GazTheDestroyer May 14 '15 at 13:49
  • In which format are you saving the file? – Magnus May 14 '15 at 13:51
  • png, gif, jpg... anything really. whatever the user uploads – Jimmyt1988 May 14 '15 at 13:51
  • 1
    Please include the code used to save the image, ie expand `Save(...)` to the true code. – David Arno May 14 '15 at 13:53
  • 2
    I don't think the settings to the graphics object matter much for the size of the file. For example when saving to jpeg you need to set the [Compression level](https://msdn.microsoft.com/en-us/library/bb882583%28v=vs.110%29.aspx) – Magnus May 14 '15 at 13:56
  • @DavidArno - That's the actual code... I'm not doing anything else, it just takes a string of where to place the file... – Jimmyt1988 May 14 '15 at 13:58
  • And for gifs the color pallet used probably makes a big difference to both quality and size. – Magnus May 14 '15 at 14:03
  • Shouldn't `g.DrawImage(image, 0, 0, width, resizeHeight);` be `g.DrawImage(b, 0, 0, width, resizeHeight);`? – Zohar Peled May 14 '15 at 14:06
  • 1
    @ZoharPeled No, `b` is already the underlying image to which the graphics objects writes the data. And the op want to draw the old image resized into the new image – Magnus May 14 '15 at 14:09
  • Magnus, thanks man... I'll post my answer code... but thanks to you I got it.. Brofist. – Jimmyt1988 May 14 '15 at 14:20

1 Answers1

2

As Magnus rightly said, the drawing to the canvas makes no difference to size... of file...

It was the save file part that was being all noob... This is what it should be:

    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);
}
Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233