Wednesday, April 11, 2007

WPF: Image Drawing

XAML:
<Image Name="image1" Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="200" />

C#:
DrawingGroup group = new DrawingGroup();
DrawingImage di = new DrawingImage(group);
//-- create clipping bound for drawing group
RectangleGeometry myRectangleGeometry = new RectangleGeometry();
myRectangleGeometry.Rect = new Rect(0,0,250,250);
group.ClipGeometry = myRectangleGeometry;
image1.Source = di;

//-- load image source (refer to prev post)
ImageSource img2 = ...

//-- to draw something
using (DrawingContext dc = group.Open())
{
dc.DrawImage(img2, new Rect(x, y, w, h));
//-- and other stuff
}

No comments: