InkCanvas to BitMap

给你一囗甜甜゛ 提交于 2019-12-08 06:24:32

问题


I have a problem with saving canvas to BMP file (or any other type).

I'm trying to save my InkCanvas like this:

int margin = (int)canvas.Margin.Left;
int width = (int)canvas.ActualWidth -margin;
int height = (int)canvas.ActualHeight -margin;
//render ink to bitmap
RenderTargetBitmap renderBitmap =
new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Default);
renderBitmap.Render(canvas);
//save the ink to a memory stream
BitmapEncoder encoder;
encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
encoder.Save(myStream);

But i always got an image with a black borders from left and top, equal size from point (0,0) of my window to my canvas. What i do wrong?


回答1:


I had a similar problem and it looks like your margins are affecting the rendering of the image.

I presume canvas is the name of the InkCanvas, so to avoid the black borders - all you need to do is amend your WPF frames and use a canvas to set up your margins instead of using the InkCanvas:

<Grid Height="340" Width="445">
   <Canvas Background="Transparent" Margin="10,10,0,0">
      <InkCanvas Name="canvas" Height="320" Width="425"/>
   </Canvas>
</Grid>



回答2:


You need to put it into a separate container (ie., into Grid).



来源:https://stackoverflow.com/questions/20623126/inkcanvas-to-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!