如何通过从源BitmapImage的中心裁剪来停止呈现为DrawingImage的椭圆裁剪 [英] How do I stop an elliptical crop rendered as a DrawingImage from cropping from the centre of the source BitmapImage
问题描述
我的代码适用于WPF .Net 4.5 Virtual Studio 2012 :
//在图像上绘制了一个圆圈,裁剪的细节在theCropRect中
< pre lang =cs> internal void GetOvalCrop(BitmapImage bi,Rect theCropRect)
{
// 创建图片
图片img = new Image();
img.HorizontalAlignment = HorizontalAlignment.Left;
img.Source = bi;
// 定义剪辑的轮廓
img.Clip = new EllipseGeometry(theCropRect);
GeometryDrawing gd = 新 GeometryDrawing();
gd.Geometry = img.Clip;
// 将剪辑的内容定义为ImageBrush
ImageBrush imb = new ImageBrush(img.Source);
imb.Stretch = Stretch.None;
gd.Brush = imb;
// 尝试将剪辑内容转换为DrawingImage
DrawingImage gImg = new DrawingImage(gd);
gImg.Freeze();
将Drawingimage转换为RenderTargetBitmap
RenderTargetBitmap rtb = DrawingImageExtensions.ToBitmapSource(gImg);
// 化妆名称
string name = Oval _ + ovalClip.ToString()+ 。png;
ovalClip ++;
// 保存结果
SaveRtbAsPng( rtb,姓名);
}
内部 静态 void SaveRtbAsPng(RenderTargetBitmap rtb, string name)
{
PngBitmapEncoder ping = new PngBitmapEncoder();
尝试
{
使用(FileStream文件= new FileStream(name,FileMode.Create,FileAccess.Write))
{
ping.Frames.Add(BitmapFrame.Create(rtb) );
ping.Save(file);
if (file!= null )
file.Close ();
}
}
catch {}
}
public static class DrawingImageExtensions
{
public static RenderTargetBitmap ToBitmapSource( this DrawingImage source)
{
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
drawingContext.DrawImage(source, new Rect( 0 , 0 ,( int )source.Width,( int )source.Height ));
drawingContext.Close();
RenderTargetBitmap bmp = new RenderTargetBitmap(( int )source.Width,( int )source.Height, 96 , 96 ,PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
return bmp;
}
public static DrawingImage ToDrawingImage(这个 BitmapSource源代码)
{
Rect imageRect = new Rect( 0 , 0 ,source.PixelWidth,source.PixelHeight);
ImageDrawing drawing = new ImageDrawing(source,imageRect);
return new DrawingImage(drawing);
}
}
结果对于高度和宽度是正确的,但椭圆的中心始终是保存裁剪的中心。
从图像左侧裁剪的圆圈最终是矩形图像中心的裁剪。
有什么想法吗?
My Code is for WPF .Net 4.5 Virtual Studio 2012:
// a circle has been drawn on an image and the cropping details are in "theCropRect"
internal void GetOvalCrop(BitmapImage bi, Rect theCropRect)
{
// create an image
Image img = new Image();
img.HorizontalAlignment = HorizontalAlignment.Left;
img.Source = bi;
//define the outline of the clip
img.Clip = new EllipseGeometry(theCropRect);
GeometryDrawing gd = new GeometryDrawing();
gd.Geometry = img.Clip;
// define the contents of the clip as an ImageBrush
ImageBrush imb = new ImageBrush(img.Source);
imb.Stretch = Stretch.None;
gd.Brush = imb;
// try to convert clip contents to a DrawingImage
DrawingImage gImg = new DrawingImage(gd);
gImg.Freeze();
convert the Drawingimage to a RenderTargetBitmap
RenderTargetBitmap rtb = DrawingImageExtensions.ToBitmapSource(gImg);
// makeup a name
string name = "Oval_" + ovalClip.ToString() + ".png";
ovalClip++;
// save results
SaveRtbAsPng(rtb, name);
}
internal static void SaveRtbAsPng(RenderTargetBitmap rtb, string name)
{
PngBitmapEncoder ping = new PngBitmapEncoder();
try
{
using (FileStream file = new FileStream(name, FileMode.Create, FileAccess.Write))
{
ping.Frames.Add(BitmapFrame.Create(rtb));
ping.Save(file);
if (file != null)
file.Close();
}
}
catch { }
}
public static class DrawingImageExtensions
{
public static RenderTargetBitmap ToBitmapSource(this DrawingImage source)
{
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
drawingContext.DrawImage(source, new Rect(0, 0, (int)source.Width, (int)source.Height));
drawingContext.Close();
RenderTargetBitmap bmp = new RenderTargetBitmap((int)source.Width, (int)source.Height, 96, 96, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
return bmp;
}
public static DrawingImage ToDrawingImage(this BitmapSource source)
{
Rect imageRect = new Rect(0, 0, source.PixelWidth, source.PixelHeight);
ImageDrawing drawing = new ImageDrawing(source, imageRect);
return new DrawingImage(drawing);
}
}
The result is correct for Height and Width but the centre of the ellipse is always the center of saved crop.
A Circle cropped from the left of the image ends up being a crop of the center of the rectangular image.
Any thoughts?
这篇关于如何通过从源BitmapImage的中心裁剪来停止呈现为DrawingImage的椭圆裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!