1

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 ですが、任意の形式にすることができます。

これは、データベースから読み込まれてトリミングされた後の画像の画像です...

プリクロップされた画像

この画像はトリミングの結果です。ご覧のとおり、良くありません。

ここに画像の説明を入力

このトピックに関する他の投稿を見たことがありますが、役に立ちませんでした。誰でも解決策を提案できますか?

ありがとうございました

4

2 に答える 2

1

そのすべてをまとめたブログです。現在、アプリケーションで動作しています。http://blog.tallan.com/2011/02/04/using-mvc3-razor-helpers-and-jcrop-to-upload-and-crop-images/

于 2012-12-09T20:46:27.620 に答える
0

Gunnar Peipman は、品質を損なうことなく画像のサイズを変更するという品質に関するブログ投稿を行っています。グンナーは、何がうまくいかないのかについていくつかのトリックを言います。

Image に埋め込まれたサムネイル画像が含まれている場合、このメソッドは埋め込まれたサムネイルを取得し、要求されたサイズにスケーリングします。Image にサムネイル画像が埋め込まれていない場合、このメソッドはメイン画像をスケーリングしてサムネイル画像を作成します。

このGetThumbnailImageメソッドは、要求されたサムネイル画像のサイズが約 120 x 120 ピクセルの場合にうまく機能します。サムネイルが埋め込まれた画像から大きなサムネイル画像 (300 x 300 など) を要求すると、サムネイル画像の品質が著しく低下する可能性があります。DrawImage メソッドを呼び出して、(埋め込まれたサムネイルをスケーリングするのではなく) メイン イメージをスケーリングする方がよい場合があります。

また、Razor 構文はJCropと友好的であり、その使用方法に関するクールなブログ投稿があります。

ここに画像の説明を入力

于 2012-12-09T17:26:40.470 に答える