Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF缩略图图像模糊_C#_Wpf - Fatal编程技术网

C# WPF缩略图图像模糊

C# WPF缩略图图像模糊,c#,wpf,C#,Wpf,我有一个WPF应用程序,它将缩略图保存为png。代码运行得很好,但当我打开图像时,图像非常模糊。它抓取的图像来自画布本身。画布根据我正在加载的图像更改其宽度和高度。所需的缩略图大小将为200 x 200(像素) 这是我的密码 public void CreateThumbail(Canvas canvas, string filename) { RenderTargetBitmap rtb = new RenderTargetBitmap( (int)

我有一个WPF应用程序,它将缩略图保存为png。代码运行得很好,但当我打开图像时,图像非常模糊。它抓取的图像来自画布本身。画布根据我正在加载的图像更改其宽度和高度。所需的缩略图大小将为200 x 200(像素)

这是我的密码

    public void CreateThumbail(Canvas canvas, string filename)
    {
      RenderTargetBitmap rtb = new RenderTargetBitmap(
        (int)canvas.ActualWidth,
        (int)canvas.ActualHeight,
        96, //dip X
        96, //dpi Y
        PixelFormats.Pbgra32);
      rtb.Render(canvas);

      PngBitmapEncoder pngImage = new PngBitmapEncoder();

      pngImage.Frames.Add(CreateResizedImage(rtb, 200, 200, 0));
      using (var filestream = System.IO.File.Create(filename))
      {
        pngImage.Save(filestream);
      }
    }

    private static BitmapFrame CreateResizedImage(ImageSource source, int width, int height, int margin)
    {
      var rect = new Rect(margin, margin, width, height);

      var group = new DrawingGroup();
      RenderOptions.SetBitmapScalingMode(group, BitmapScalingMode.HighQuality);
      group.Children.Add(new ImageDrawing(source, rect));

      var drawingVisual = new DrawingVisual();
      using (var drawingContext = drawingVisual.RenderOpen())
        drawingContext.DrawDrawing(group);

      var resizedImage = new RenderTargetBitmap(
          (int)rect.Width, (int)rect.Height,// Resized dimensions
          96, 96, // Default DPI values
          PixelFormats.Pbgra32); // Default pixel format
      resizedImage.Render(drawingVisual);

      return BitmapFrame.Create(resizedImage);
    }

在调整图像大小之前,我保存了它,它看起来又清晰又锐利。然而,当我保存缩略图时,它又丑又模糊。我做错了什么?我是不是设计过度了?非常感谢。

您可能需要尊重图像的原始尺寸。例如,400x400的图像将很好地缩小到200x200,但235x235的图像则不会

<>这不考虑图像不是正方形开始。

您可以尝试将图像的高度和宽度减少一个好的因子(我将从减半开始),直到图像小于200x200,然后用白色或透明填充


图像处理可能相当困难。这两个方面我都不是专家,所以我可能会尝试一下像我在谷歌上找到的第三方库:

可能你需要尊重图像的原始尺寸。例如,400x400的图像将很好地缩小到200x200,但235x235的图像则不会

<>这不考虑图像不是正方形开始。

您可以尝试将图像的高度和宽度减少一个好的因子(我将从减半开始),直到图像小于200x200,然后用白色或透明填充

图像处理可能相当困难。这两方面我都不是专家,所以我可能会尝试一个第三方库,就像我刚刚在谷歌上找到的一样: