1

を使用して画像のサイズを変更していbitmap.GetThumbnailImage()ますが、画質/解像度が低下しているようです。たとえば、元の画像の解像度は 300dpi で、サイズ変更された 1 ははるかに小さくなっています。

サイズ変更時に画像の解像度を維持するにはどうすればよいですか?

4

2 に答える 2

1

これは、画像を縮小するために使用する関数です。品質は優れています。

    private static Image ResizeImage(Image imgToResize, Size size)
    {
        int sourceWidth = imgToResize.Width;
        int sourceHeight = imgToResize.Height;

        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;

        nPercentW = ((float)size.Width / (float)sourceWidth);
        nPercentH = ((float)size.Height / (float)sourceHeight);

        if (nPercentH < nPercentW)
            nPercent = nPercentH;
        else
            nPercent = nPercentW;

        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);

        Bitmap b = new Bitmap(destWidth, destHeight);
        Graphics g = Graphics.FromImage((Image)b);
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;

        g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
        g.Dispose();

        return (Image)b;
    }

使用方法は次のとおりです。

        int length = (int)stream.Length;
        byte[] tempImage = new byte[length];
        stream.Read(tempImage, 0, length);

        var image = new Bitmap(stream);
        var resizedImage = ResizeImage(image, new Size(300, 300));

あなたがそれを実行するのに助けが必要な場合は、叫び声をあげてください。

于 2011-05-20T17:42:37.437 に答える
1

InterpolationModeの設定を見てください(MSDN リンク)

このリンクもご覧ください:高品質のサムネイルを作成する - 画像を動的にサイズ変更する

基本的に、次のようなコードがあります: Bitmap imageToScale = new bitmap( //縮小したい画像でこれを終了します

Bitmap bitmap = new Bitmap(imgWidth, imgHeight);  

using (Graphics graphics = Graphics.FromImage(result))
{
    graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
    graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    graphics.DrawImageimageToScale, 0, 0, result.Width, result.Height);
    bitmap.Save(memoryStreamNew, System.Drawing.Imaging.ImageFormat.Png);
} 

bitmap.Save( //finish this depending on if you want to save to a file location, stream, etc...
于 2011-05-20T14:17:34.407 に答える