以前做winform的时候了解过画图,用Graphics在Bitmap上画:
Bitmap bitmap = new Bitmap(...);
Graphics g = Graphics.FromImage(bitmap)
然后可以用g来draw图形
也可以通过g.CopyFromScreen来截图
通过bitmap.Save可以保存到本地
最后bitmap.Dispose() g.Dispose()
而WPF的画图是很强的,很多类都重写过,不再用System.Drawing命名空间而是在System.Windows.Media命名空间里
今天想看下保存几何图的方法,对WPF的画图用得不是太多,刚看到这么多类,还有点晕,查看msdn再加上网上查阅
简单关系如下:
System.Windows.Control.Image 是一个控件类
Source属性 System.Windows.Media.ImageSource Image类的Source属性,抽象类
System.Windows.Media.DrawingImage 继承自ImageSource
Drawing属性 System.Windows.Media.Drawing
System.Windows.Media.GeometryDrawing
Geometry属性 就是几何形状 EllipseGeometry GeometryGroup LineGeometry PathGeometry GectangleGeometry...
System.Windows.Media.ImageDrawing
System.Windows.Media.Imaging.BitmapSource 继承自ImageSource
通过下面的方法可以保存图片
public static void SaveBitmapImage(BitmapImage bitmap, string filePath)
{
BitmapEncoder encoder =new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
{
encoder.Save(fileStream);
}
}