Im using this code to compress the image and convert to byte.
@using System.Drawing;
@using System.Drawing.Imaging;
@using System.Drawing.Drawing2D;
@functions {
public static Image GenerateThumbnails(double scaleFactor, Image image)
{
var newWidth = (int)(image.Width * scaleFactor);
var newHeight = (int)(image.Height * scaleFactor);
var thumbnailImg = new Bitmap(newWidth, newHeight);
var thumbGraph = Graphics.FromImage(thumbnailImg);
thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
thumbGraph.DrawImage(image, imageRectangle);
return thumbnailImg;
}
public static byte[] ImageToByte(Image img)
{
byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, ImageFormat.Png);
stream.Close();
byteArray = stream.ToArray();
}
return byteArray;
}
}
I want to use .RawFormat to keep the source image format. So i tried to change the code but does not work. Is there a way to preserve the image format based on the source image?
public static byte[] ImageToByte(Image img)
{
byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, img.RawFormat);
stream.Close();
byteArray = stream.ToArray();
}
return byteArray;
}
Instead please tell explicitely which error message you have (or the best English translation) or which bad behavior you see. What fails when you change just this line? You are trying with really the same PNG file ?
For now my guess is that you later still serve this image with a png mime type even if this is not really a png file which may confuse the browser? Also you close the Stream before using the Stream again and anyway you have a using statement (but if it worked
fine previously it might be not a problem for now). Bitmap and Graphics are not disposed.
Knowing what is the behavior of the code you are showing us would likely help to better understand your issue.
As you see the issue is that you are not telling is wha
Member
574 Points
652 Posts
Convert image to byte, preserve source format
Apr 07, 2017 05:25 PM|dow7|LINK
Im using this code to compress the image and convert to byte.
I want to use .RawFormat to keep the source image format. So i tried to change the code but does not work. Is there a way to preserve the image format based on the source image?
All-Star
48290 Points
17987 Posts
Re: Convert image to byte, preserve source format
Apr 07, 2017 05:44 PM|PatriceSc|LINK
Hi,
Instead please tell explicitely which error message you have (or the best English translation) or which bad behavior you see. What fails when you change just this line? You are trying with really the same PNG file ?
For now my guess is that you later still serve this image with a png mime type even if this is not really a png file which may confuse the browser? Also you close the Stream before using the Stream again and anyway you have a using statement (but if it worked fine previously it might be not a problem for now). Bitmap and Graphics are not disposed.
Knowing what is the behavior of the code you are showing us would likely help to better understand your issue.
As you see the issue is that you are not telling is wha
Member
574 Points
652 Posts
Re: Convert image to byte, preserve source format
Apr 07, 2017 08:02 PM|dow7|LINK
[ArgumentNullException: Value cannot be null.Parameter name: encoder] System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) +475641 System.Drawing.Image.Save(Stream stream, ImageFormat format) +36 ASP.CompressImg.ImageToByte(Image img)
All-Star
48290 Points
17987 Posts
Re: Convert image to byte, preserve source format
Apr 08, 2017 08:10 AM|PatriceSc|LINK
Ok got it. The problem is that the RawFormat for the new bitmap is null as it was not loaded from a file.
You could pass the format as an addtional parameter to take the format of the original source file rather than from the newly created thumbnail.