将WPF输出写入图像文件

时间:2022-02-23 14:07:34

Is there a way I can write the output of WPF say a canvas to a image file, jpg or the like.

有没有办法我可以写WPF的输出说画布到图像文件,jpg等。

I want to use WPF to create a background for me as I want to use the BitmapEffects for a rectangle and also radius the corners.

我想使用WPF为我创建一个背景,因为我想将BitmapEffects用于矩形并且也使角落半径。

I want to use the bitmap in a webpage.

我想在网页中使用位图。

Is this possible?

这可能吗?

Malcolm

马尔科姆

1 个解决方案

#1


9  

I have a blog post all about this here. Here's the code from the article:

我在这里有关于此的博客文章。这是文章中的代码:

   Rect rect = new Rect(canvas.RenderSize);
   RenderTargetBitmap rtb = new RenderTargetBitmap((int)rect.Right,
     (int)rect.Bottom, 96d, 96d, System.Windows.Media.PixelFormats.Default);
   rtb.Render(canvas);
   //encode as PNG
   BitmapEncoder pngEncoder = new PngBitmapEncoder();
   pngEncoder.Frames.Add(BitmapFrame.Create(rtb));

   //save to memory stream
   System.IO.MemoryStream ms = new System.IO.MemoryStream();

   pngEncoder.Save(ms);
   ms.Close();
   System.IO.File.WriteAllBytes("logo.png", ms.ToArray());
   Console.WriteLine("Done");

#1


9  

I have a blog post all about this here. Here's the code from the article:

我在这里有关于此的博客文章。这是文章中的代码:

   Rect rect = new Rect(canvas.RenderSize);
   RenderTargetBitmap rtb = new RenderTargetBitmap((int)rect.Right,
     (int)rect.Bottom, 96d, 96d, System.Windows.Media.PixelFormats.Default);
   rtb.Render(canvas);
   //encode as PNG
   BitmapEncoder pngEncoder = new PngBitmapEncoder();
   pngEncoder.Frames.Add(BitmapFrame.Create(rtb));

   //save to memory stream
   System.IO.MemoryStream ms = new System.IO.MemoryStream();

   pngEncoder.Save(ms);
   ms.Close();
   System.IO.File.WriteAllBytes("logo.png", ms.ToArray());
   Console.WriteLine("Done");