码迷,mamicode.com
首页 > Windows程序 > 详细

C# wpf InkCanvas 保存图片jpg

时间:2018-03-08 10:37:28      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:code   pos   height   nav   render   component   通过   res   保存   

技术分享图片

技术分享图片

前端xaml页面代码

<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        
        <Canvas x:Name="Screen" Width="96  " Height="96">
            <InkCanvas x:Name="inkCanvas" Height="299" Width="502" Canvas.Left="-203" Canvas.Top="-108" />
            <TextBlock Canvas.Left="200" Canvas.Top="50" x:Name="VSSize" ></TextBlock>
        </Canvas>
       
        <Button Content="保存" Height="25" HorizontalAlignment="Right" Margin="0,274,0,12" Name="button1" Width="75" Click="button1_Click" />
    </Grid>
</Window>

后台cs页面代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Ink;

namespace WpfApplication6
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        DrawingAttributes drawingAttributes;  
        public MainWindow()
        {
            InitializeComponent();
            //创建 DrawingAttributes 类的一个实例  
            drawingAttributes = new DrawingAttributes();
            //将 InkCanvas 的 DefaultDrawingAttributes 属性的值赋成创建的 DrawingAttributes 类的对象的引用  
            //InkCanvas 通过 DefaultDrawingAttributes 属性来获取墨迹的各种设置,该属性的类型为 DrawingAttributes 型  
            inkCanvas.DefaultDrawingAttributes = drawingAttributes;
            //设置 DrawingAttributes 的 Color 属性设置颜色  
            drawingAttributes.Color = Colors.Red;  
        }

       

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            RenderTargetBitmap rtb = new RenderTargetBitmap(96, 96, this.Screen.Width, this.Screen.Height, PixelFormats.Default);
            rtb.Render(this.inkCanvas);
            BmpBitmapEncoder encoder = new BmpBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(rtb));
            string file = "d:\\xxx.jpg";
            using (Stream stm = File.Create(file))
            {
                encoder.Save(stm);
            }
        }

    }
}

 

C# wpf InkCanvas 保存图片jpg

标签:code   pos   height   nav   render   component   通过   res   保存   

原文地址:https://www.cnblogs.com/wzl521224/p/8526739.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!