问题描述
我需要从strokeCollection获取图像,但是由于mvvm结构而无法访问Visual(InkCanvas)本身。有没有可能简单的方法来完成此任务?
XAML:
<InkCanvas x:Name="paintSurface" Grid.Row="0" Opacity="0.2" strokes="{Binding strokes}">
<InkCanvas.DefaultDrawingAttributes>
<DrawingAttributes Color="Black" Width="10" Height="10"/>
</InkCanvas.DefaultDrawingAttributes>
</InkCanvas>
viewmodel:
private strokeCollection _strokes;
public strokeCollection strokes {
get => _strokes;
set => SetProperty(ref _strokes,value);
}
现在,我只想将strokeCollection转换为某种形式的可处理图像,它是否是位图,位图图像,Mat,EmguCV图像并不重要。 提前致谢:)
解决方法
这是您的答案。
将StrokeCollection保存为所有格式的图像。
https://stackoverflow.com/a/51207941/7300644
我通过将StrokeCollection交给一个帮助器类解决了我的问题,在该类中,我只是创建了一个新的InkCanvas,添加了Strokes(来自UI中的原始InkCanvas)并进行渲染。
public static Bitmap convertStrokestoImage(StrokeCollection strokes,int width,int height)
{
InkCanvas InkyStinky = new InkCanvas();
InkyStinky.RenderSize = new System.Windows.Size(width,height);
InkyStinky.Strokes.Add(strokes);
RenderTargetBitmap bmp = new RenderTargetBitmap(width,height,96,PixelFormats.Pbgra32);
bmp.Render(InkyStinky);
MemoryStream stream = new MemoryStream();
BitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
encoder.Save(stream);
Bitmap b = new Bitmap(stream);
return b;
}