从 C# 中的 StrokeCollection 渲染图像
Render an Image from a StrokeCollection in c#
我需要从 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>
视图模型:
private StrokeCollection _strokes;
public StrokeCollection Strokes {
get => _strokes;
set => SetProperty(ref _strokes, value);
}
现在我只想将 StrokeCollection 转换成某种形式的可处理图像,无论是位图、BitmapImage、Mat、EmguCV 图像,都不重要。
提前致谢:)
这是你的答案。
将 StrokeCollection 保存为所有格式的图像。
我通过将 StrokeCollection 交给助手 class 解决了我的问题,我只是在其中创建了一个新的 InkCanvas,添加了笔触(来自 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, 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;
}
我需要从 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>
视图模型:
private StrokeCollection _strokes;
public StrokeCollection Strokes {
get => _strokes;
set => SetProperty(ref _strokes, value);
}
现在我只想将 StrokeCollection 转换成某种形式的可处理图像,无论是位图、BitmapImage、Mat、EmguCV 图像,都不重要。 提前致谢:)
这是你的答案。
将 StrokeCollection 保存为所有格式的图像。
我通过将 StrokeCollection 交给助手 class 解决了我的问题,我只是在其中创建了一个新的 InkCanvas,添加了笔触(来自 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, 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;
}