MVC3 C# アプリケーションで画像をトリミングできません。画像をトリミングしていますが、ビューにレンダリングすると品質が低下します。トリミングする画像はデータベースからロードされ、ByteArray から作成されます...
public static Image ByteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
この画像がビューにレンダリングされると、見た目が良く、期待される品質から外れます。次に、トリミングする領域を選択し、以下の方法を使用してトリミングを行います...
public Image CropImage(System.Drawing.Image Image, int Height, int Width, int StartAtX, int StartAtY)
{
Image outimage;
MemoryStream mm = null;
try
{
//check the image height against our desired image height
if (Image.Height < Height)
{
Height = Image.Height;
}
if (Image.Width < Width)
{
Width = Image.Width;
}
//create a bitmap window for cropping
Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(Image.VerticalResolution,Image.HorizontalResolution);
//create a new graphics object from our image and set properties
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
//now do the crop
grPhoto.DrawImage(Image, new Rectangle(0, 0, Width, Height), StartAtX, StartAtY, Width, Height, GraphicsUnit.Pixel);
// Save out to memory and get an image from it to send back out the method.
mm = new MemoryStream();
bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
Image.Dispose();
bmPhoto.Dispose();
grPhoto.Dispose();
outimage = Image.FromStream(mm);
return outimage;
}
catch (Exception ex)
{
throw new Exception("Error cropping image, the error was: " + ex.Message);
}
}
このトリミングされた画像は ByteArray に変換され、次のようにデータベースに保存できます...
public static byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
この画像が後でビューにレンダリングされると、元の画像よりも品質が大幅に低下します。それはかなり頭がおかしいように見えます。この場合の元の画像は .jpg ですが、任意の形式にすることができます。
これは、データベースから読み込まれてトリミングされた後の画像の画像です...
この画像はトリミングの結果です。ご覧のとおり、良くありません。
このトピックに関する他の投稿を見たことがありますが、役に立ちませんでした。誰でも解決策を提案できますか?
ありがとうございました