RenderTargetBitmap RenderVisaulToBitmap(Visual vsual, int width, int height)
{
var rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
rtb.Render(vsual);
return rtb;
}
public enum ImageFormat { JPG, BMP, PNG, GIF, TIF }
//图片存入流
void GenerateImage(BitmapSource bitmap, ImageFormat format, Stream destStream)
{
BitmapEncoder encoder = null;
switch (format)
{
case ImageFormat.JPG:
encoder = new JpegBitmapEncoder();
break;
case ImageFormat.PNG:
encoder = new PngBitmapEncoder();
break;
case ImageFormat.BMP:
encoder = new BmpBitmapEncoder();
break;
case ImageFormat.GIF:
encoder = new GifBitmapEncoder();
break;
case ImageFormat.TIF:
encoder = new TiffBitmapEncoder();
break;
default:
throw new InvalidOperationException();
}
encoder.Frames.Add(BitmapFrame.Create(bitmap));
encoder.Save(destStream);
}
//PickColorPlateImage是需要被截取的控件
void CatchImg()
{
Stream fs = File.Create("F:\\test.bmp");
var bmp = RenderVisaulToBitmap(PickColorPlateImage, (int)PickColorPlateImage.Width, (int)PickColorPlateImage.Height);
ImageFormat formate = ImageFormat.BMP;
BitmapSource bit = bmp;
GenerateImage(bit, formate, fs);
}
,