0

WPF を使用して画像のサイズを変更すると、一部の解像度で画像がぼやけてしまうという問題が発生しています。私は実際にこれらをファイルに書き込んでいるので、SnapToDevicePixels役に立ちません(私はWPFアプリでもなく、参照しているだけですSystem.Windows)。

これは WPF のピクセルのデバイス非依存性に関連していることを理解していますが、鮮明な画像を取得するためにピクセルのオフセットを計算する方法を知る必要があります。

4

1 に答える 1

0

WPF を使用する必要はありますか? 私たちが使用するこの GDI ベースのコードは、優れたサイズ変更機能を生み出します。

    public static Size ResizeImage(
       string fileName, 
       string targetFileName, 
       Size boundingSize, 
       string targetMimeType, 
       long quality)
    {
        ImageCodecInfo imageCodecInfo = 
            ImageCodecInfo
               .GetImageEncoders()
               .Single(i => i.MimeType == targetMimeType);
        EncoderParameters encoderParams = new EncoderParameters(1);
        encoderParams.Param[0] = 
            new EncoderParameter(Encoder.Quality, quality);
        using (FileStream fs = File.OpenRead(fileName))
        {
            Image img ;
            try
            {
                img = Image.FromStream(fs, true, true);
            }
            catch (ArgumentException ex)
            {
                throw new FileFormatException("cannot decode image",ex);
            } 
            using (img)
            {
                double targetAspectRatio = 
                    ((double)boundingSize.Width) / boundingSize.Height;
                double srcAspectRatio = ((double)img.Width) / img.Height;
                int targetWidth = boundingSize.Width;
                int targetHeight = boundingSize.Height;
                if (srcAspectRatio > targetAspectRatio)
                {
                    double h = targetWidth / srcAspectRatio;
                    targetHeight = Convert.ToInt32(Math.Round(h));
                }
                else
                {
                    double w = targetHeight * srcAspectRatio;
                    targetWidth = Convert.ToInt32(Math.Round(w));
                }
                using (Image thumbNail = new Bitmap(targetWidth, targetHeight))
                using (Graphics g = Graphics.FromImage(thumbNail))
                {
                    g.CompositingQuality = CompositingQuality.HighQuality;
                    g.SmoothingMode = SmoothingMode.HighQuality;
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    Rectangle rect = 
                        new Rectangle(0, 0, targetWidth, targetHeight);
                    g.DrawImage(img, rect);
                    thumbNail.Save(
                        targetFileName, imageCodecInfo, encoderParams);
                }
                return new Size(targetWidth, targetHeight);
            }

        }
    }
于 2012-11-12T22:58:18.310 に答える