jpeg 画像をアップロードし、バイト配列としてデータベースに保存しています。ただし、画像を取得して「名前を付けて保存」を選択すると、画像はPNGになり、ファイルサイズは60〜70%大きくなります。これが変換を行う関数です。なぜこれが起こっているのか誰にも示唆できますか?
private byte[] ResizeImage(UploadedFile file, int maxWidth, int maxHeight)
{
int canvasWidth = maxWidth;
int canvasHeight = maxHeight;
using (Bitmap originalImage = new Bitmap(file.InputStream))
{
int originalWidth = originalImage.Width;
int originalHeight = originalImage.Height;
Bitmap thumbnail = new Bitmap(canvasWidth, canvasHeight); // create thumbnail canvas
using (Graphics g = Graphics.FromImage((System.Drawing.Image)thumbnail))
{
g.SmoothingMode = SmoothingMode.Default;
g.InterpolationMode = InterpolationMode.Default;
g.PixelOffsetMode = PixelOffsetMode.Default;
//Get the ratio of original image
double ratioX = (double)canvasWidth / (double)originalWidth;
double ratioY = (double)canvasHeight / (double)originalHeight;
double ratio = ratioX < ratioY ? ratioX : ratioY; // use which ever multiplier is smaller
// Calculate new height and width
int newHeight = Convert.ToInt32(originalHeight * ratio);
int newWidth = Convert.ToInt32(originalWidth * ratio);
// Calculate the X,Y position of the upper-left corner
// (one of these will always be zero)
int posX = Convert.ToInt32((canvasWidth - (originalWidth * ratio)) / 2);
int posY = Convert.ToInt32((canvasHeight - (originalHeight * ratio)) / 2);
//g.Clear(Color.White); // Add white padding
g.DrawImage(originalImage, posX, posY, newWidth, newHeight);
}
/* Display uploaded image preview */
ImageConverter converter = new ImageConverter();
byte[] imageData = (byte[])converter.ConvertTo(thumbnail, typeof(byte[])); // Convert thumbnail into byte array and set new binary image
return imageData;
}
}