绘制在WPF图像控制 [英] Draw on image control in WPF

查看:228
本文介绍了绘制在WPF图像控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建应用程序,它可以加载一些图像,应用一些滤镜,画几行并保存。我必须用WPF做。怎样绘制在WPF图像控制?或其他控制是更好?


解决方案

您可以通过添加InkCanvas到您的网页做到这一点,添加您的图像为背景图片。InkCanvas,并添加保存功能




  1. 添加InkCanvas到您的WPF形式:

    1. 打开工具箱(在视图菜单上,单击工具箱)

    2. 右键单击工具箱,然后单击选择项(选择工具箱项对话框打开箱)

    3. 在选择工具箱项对话框的WPF组件选项卡,向下滚动到InkCanvas,并让检查出现在复选框选中它。

    4. 单击OK添加InkCanvas控制到工具箱。

    5. 从工具箱中拖动一个InkCanvas控制,以设计图面。


  2. 添加您的图像作为InkCanvas的背景。

    $ B $:您可以在属性窗口下的背景或XAML

  3. 添加保存按钮,将形式和使用下面的代码保存它做b

     字符串newImagePath =你的文件路径; 
    变种MS =新的MemoryStream();使用

    (的FileStream FS =新的FileStream(newImagePath,FileMode.Create)
    {
    变种RTB =新RenderTargetBitmap((INT)inkImageCanvas.Width,(INT)inkImageCanvas.Height ,96D,96D,PixelFormats.Default);
    rtb.Render(inkImageCanvas);
    JpegBitmapEncoder编码器=新JpegBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(RTB)) ;
    encoder.Save(FS);
    }




newImagePath 是路径到新文件; inkImageCanvas 是InkCanvas控制

这将节省您的inkCanval为JPG文件的内容。


I must create application, where it can be to load some image, apply some filters, draw few lines and save it. And I must do it using WPF. How I can draw on Image control in WPF? Or another control is better for it?

解决方案

You can do this by adding InkCanvas to your page, add your image as Background Image of InkCanvas, and add save functionality.

  1. Add InkCanvas to your WPF Form:

    1. Open the Toolbox (on the View menu, click Toolbox)
    2. Right-click the Toolbox, and then click Choose Items (Choose Toolbox Items dialog box opens)
    3. On the WPF Components tab of the Choose Toolbox Items dialog box, scroll down to InkCanvas and select it so that a check appears in the check box.
    4. Click OK to add the InkCanvas control to the Toolbox.
    5. Drag an InkCanvas control from the Toolbox to the design surface.

  2. Add your image as a background of InkCanvas. You can do it either in properties window under Background or in XAML
  3. Add "Save" button to your form and use following code to save it:

    string newImagePath = "your file path";
    var ms = new MemoryStream();             
    
    using (FileStream fs = new FileStream(newImagePath , FileMode.Create)
    {
        var rtb = new RenderTargetBitmap((int)inkImageCanvas.Width, (int)inkImageCanvas.Height, 96d, 96d, PixelFormats.Default);             
        rtb.Render(inkImageCanvas);             
        JpegBitmapEncoder encoder = new JpegBitmapEncoder();             
        encoder.Frames.Add(BitmapFrame.Create(rtb));              
        encoder.Save(fs);             
    }
    

newImagePath is the path to new file; inkImageCanvas is your InkCanvas control.

This will save content of your inkCanval to jpg file.

这篇关于绘制在WPF图像控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆