I have a user gallery at the site and it is possible for visitors to upload some images. After upload image should be resized to some predefined presets. In addition original image should be saved too. All works fine for png and bmp image formats. But if I upload gif format or jpeg with a predominance of one color uploaded original image seems to be compressed.
For example:
Original:

Uploaded:

I thorougly searched in google and found some examples how to upload image right.
At the end I have written the next method:
void UploadImagePreset(Image image, ImageFormat imgFormat, string imgPath)
{
using (var uploadStream = imageUploader.CreateUploadStream(imagePath))
{
var newWidth = image.Width;
var newHeight = image.Height;
using (var outBitmap = new Bitmap(newWidth, newHeight))
{
using (var outGraph = Graphics.FromImage(outBitmap))
{
outGraph.CompositingQuality = CompositingQuality.HighQuality;
outGraph.SmoothingMode = SmoothingMode.HighQuality;
outGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
outGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRect = new Rectangle(0, 0, newWidth, newHeight);
outGraph.DrawImage(image, imageRect);
outBitmap.Save(uploadStream, imgFormat);
}
}
}
}
But it did not help. Uploaded image was the same as described above.
I have tryied to specify qaulity of the results image like this:
var encoderParameters = new EncoderParameters();
var encoderParameter = new EncoderParameter(Encoder.Quality, 100);
encoderParameters.Param[0] = encoderParameter;
var encoder = ImageCodecInfo.GetImageEncoders()
.Where(e => e.FormatID == imgFormat.Guid)
.FirstOrDefault();
outBitmap.Save(uploadStream, encoder, encoderParameters);
It does not work too. In addition when I upload jpeg image exception occurs.
Is any way to upload image without compressing or resizing? I have spent some hours trying solve this issue and stuck on it.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You are passing the uploaded image through the Image class – this is a new image, based on the passed in stream.
This is then saved using some default parameters, which would no longer reflect the original image.
You should simply save the incoming stream directly to disk – this will result in an exact copy of the original image.
Like this (.NET 4.0):
using (var uploadStream = imageUploader.CreateUploadStream(imagePath))
{
using(var fileStream = new FileStream(imgPath, FileMode.Create))
{
uploadStream.CopyTo(fileStream);
}
}
Or (pre .NET 4.0):
using (var uploadStream = imageUploader.CreateUploadStream(imagePath))
{
using(var fileStream = new FileStream(imgPath, FileMode.Create))
{
byte[] buffer = new byte[32768];
int read;
while ((read = uploadStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, read);
}
}
}
Adapted from this answer.
Method 2
I suggest using Image Codecs (Your second example) for lossless image compression.
With a small modification of code i suggest that you try this :
Bitmap bm = (Bitmap)Image.FromFile(filePath);
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo codecInfo = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == "image/jpeg")
codecInfo = codec;
}
EncoderParameters ep = new EncoderParameters();
ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);
bm.Save("C:\quality" + x.ToString() + ".jpg", codecInfo, ep);
Hope this will help.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0