문제

I'm using a really basic fileupload for a small page that just needs an basic one.

But the issue here is that the file size increases after being uploaded to the server, to around 800 kilobyte to 1,7 megabyte

This is presumably because we're not actually uploading the file itself, just copying the stream and making a new image based on it.

I've tried to change the graphicsHandle.InterpolationMode and also tried to add graphicsHandle.CompositingQuality to change various qualities but they're always the same in size and quality for some reason, like that it doesn't affect the image, just like as if the graphicsHandle doesn't do anything.

The code I'm using is the following

Stream stream = imageUpload.PostedFile.InputStream;
Bitmap image = new Bitmap(stream);

System.Drawing.Image newImage = new Bitmap(image.Width, image.Height);
using (Graphics graphicsHandle = Graphics.FromImage(newImage))
{
     graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
     //graphicsHandle.CompositingQuality = CompositingQuality.Default;
     graphicsHandle.DrawImage(image, 0, 0, image.Width, image.Height);
}


DateTime date = DateTime.Now;
string dateString = date.ToString("yyyyMMdd_HHmmss");
Guid guid = Guid.NewGuid();

fileName = string.Format("{0}_{1}{2}", dateString, guid, extension);

newImage.Save(path + fileName);
response = true;

imageUpload is an asp:FileUpload

Everything works great except for the fact that the file size doubles.

Does the graphicsHandle just not run? since it doesn't seem to affect the image in any way

도움이 되었습니까?

해결책 2

After it has been uploaded your code saves it to an uncompressed BMP.
To fix this issue take a look at the following: http://msdn.microsoft.com/en-us/library/ytz20d80(v=vs.110).aspx
Here they explain how to save the image to a jpeg or any other image type, including compressing! I recommend using jpeg for the size convenience.

I hope this helps you out a bit!

다른 팁

You are saving the image as a bitmap. This would explain the increase in file size seeing bitmaps are uncompressed.

If you take a quick look at the msdn here: http://msdn.microsoft.com/en-us/library/9t4syfhh(v=vs.110).aspx and http://msdn.microsoft.com/en-us/library/system.drawing.imaging.imageformat(v=vs.110).aspx

you can see you can add a parameter to newImage.Save to define the format to save in.

so your code would become something like this:

newImage.Save(path + fileName, System.Drawing.Imaging.ImageFormat.Jpeg);

In order to change size of the file you can try to save new image adjusting its properties such as CompositingQuality, InterpolationMode, and quality and compression. For example, CompositingQuality could be set to "HighSpeed" value, InterpolationMode to "Low", etc. It all depends on what kind of images you have and it needs to be tested.

As an example of the code

Stream stream = imageUpload.PostedFile.InputStream;
Bitmap source = new Bitmap(stream);

Bitmap target = new Bitmap(source.Width, source.Height);
Graphics g = Graphics.FromImage(target); 

EncoderParameters e;
g.CompositingQuality = CompositingQuality.HighSpeed; <-- here
g.InterpolationMode = InterpolationMode.Low; <-- here 

Rectangle recCompression = new Rectangle(0, 0, source.Width, source.Height);
g.DrawImage(source, recCompression);

e = new EncoderParameters(2);
e.Param[0] = new EncoderParameter(Encoder.Quality, 70); <-- here 70% quality
e.Param[1] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionLZW); <-- here

target.Save(newName, GetEncoderInfo("image/jpeg"), e);

g.Dispose();
target.Dispose();

public static ImageCodecInfo GetEncoderInfo(string sMime)
{
   ImageCodecInfo[] objEncoders;
   objEncoders = ImageCodecInfo.GetImageEncoders();
   for (int iLoop = 0; iLoop <= (objEncoders.Length - 1); iLoop++)
   {
       if (objEncoders[iLoop].MimeType == sMime)
          return objEncoders[iLoop];
   }
   return null;
}

Hope this helps.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top