C# wpf InkCanvas 保存圖片jpg
阿新 • • 發佈:2018-03-08
通過 create 頁面 app init width avi alignment end
前端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