Está en la página 1de 5

Hi sainad, I code a sample for you: Xaml: <DockPanel> <Canvas Background="Yellow" Width="auto" Height="auto" Name="canvas1" MouseDow n="canvas1_MouseDown" MouseMove="canvas1_MouseMov

e" MouseUp="canvas1_MouseUp"> <Line X1="0" X2="400" Y1="0" Y2="400" Stroke="Blue" Height="262" Width="278" Str okeThickness="5"></Line> <Line X1="0" X2="200" Y1="0" Y2="400" Stroke="Red" Height="262" Width="278" StrokeThickness="5"></Line> <Line X1="0" X2="400" Y1="0" Y2="200" Stroke="Green" Height="262" Width="278 " StrokeThickness="5"></Line> </Canvas> </DockPanel>

Code-Behind: public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); }

Point downPoint; Point upPoint; Point currentPoint; bool isMouseDown = false; Rectangle rubberBand = null; string savePath = @"c:\canvas1.jpg";

string selectPath = @"c:\select1x.jpg"; private double zoomFactor = 1.0;

private void canvas1_MouseDown(object sender, MouseButtonEventArgs e) { if (!this.canvas1.IsMouseCaptured) {

isMouseDown = true; downPoint = new Point(); downPoint = e.GetPosition(this.canvas1); }

private void canvas1_MouseMove(object sender, MouseEventArgs e) {

if (isMouseDown == true) {

if (rubberBand == null) { rubberBand = new Rectangle(); rubberBand.Stroke = Brushes.LightCoral; rubberBand.StrokeDashArray = new DoubleCollection(new double[] { 4, 2 }); canvas1.Children.Add(rubberBand); }

currentPoint = e.GetPosition(this.canvas1);

double width = Math.Abs(downPoint.X - currentPoint.X); double height = Math.Abs(downPoint.Y - currentPoint.Y); double left = Math.Min(downPoint.X, currentPoint.X); double top = Math.Min(downPoint.Y, currentPoint.Y); rubberBand.Width = width; rubberBand.Height = height; Canvas.SetLeft(rubberBand, left); Canvas.SetTop(rubberBand, top);

} } private void canvas1_MouseUp(object sender, MouseButtonEventArgs e) {

if (!this.canvas1.IsMouseCaptured) {

isMouseDown = false; upPoint = new Point(); upPoint = e.GetPosition(this.canvas1); CanvasToJpg(downPoint, upPoint); RectangleToJpg();

} }

private void CanvasToJpg(Point pt1, Point pt2)

{ RenderTargetBitmap bmp = new RenderTargetBitmap((int)canvas1.ActualWidth, (in t)canvas1.ActualHeight, 96, 96, PixelFormats.Pbgra32); bmp.Render(canvas1); string Extension = System.IO.Path.GetExtension(savePath).ToLower(); BitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bmp)); using (System.IO.Stream stm = System.IO.File.Create(savePath)) { encoder.Save(stm); } } private void RectangleToJpg() { double rubberBandLeft = Canvas.GetLeft(rubberBand); double rubberBandTop = Canvas.GetTop(rubberBand); using (System.Drawing.Bitmap source = new System.Drawing.Bitmap(savePath)) {

using (System.Drawing.Bitmap target = new System.Drawing.Bitmap((int)rubberBand.Width, (int)rubberBand.Height)) { System.Drawing.RectangleF recDest = new System.Drawing.RectangleF (0.0f, 0.0f, (float)target.Width, (float)target.Height); float hd = 1.0f / (target.HorizontalResolution / source.HorizontalResolution);

float vd = 1.0f / (target.VerticalResolution / source.VerticalResolution); float hScale = 1.0f / (float)zoomFactor; float vScale = 1.0f / (float)zoomFactor; System.Drawing.RectangleF recSrc = new System.Drawing.RectangleF ((hd * (float)rubberBandLeft) * hScale, (vd * (float)rubberBandTop) * vScale, (hd * (float)rubberBand.Width) * hScale, (vd * (float)rubberBand.Height) * vScale); using (System.Drawing.Graphics gfx = System.Drawing.Graphics.FromImage(target)) { gfx.DrawImage(source, recDest, recSrc, System.Drawing.GraphicsUnit.Pixel); }

target.Save(selectPath, System.Drawing.Imaging.ImageFormat.Jpeg); } } }

También podría gustarte