private bool SaveImageFile(Drawing drawing, string fileName, string imageFileName) { string outputExt = this.GetImageFileExtention(); string outputFileName = null; if (String.IsNullOrEmpty(imageFileName)) { string fileNameWithoutExt = Path.GetFileNameWithoutExtension(fileName); string workingDir = Path.GetDirectoryName(fileName); outputFileName = Path.Combine(workingDir, fileNameWithoutExt + outputExt); } else { string fileExt = Path.GetExtension(imageFileName); if (String.IsNullOrEmpty(fileExt)) { outputFileName = imageFileName + outputExt; } else if (!String.Equals(fileExt, outputExt, StringComparison.OrdinalIgnoreCase)) { outputFileName = Path.ChangeExtension(imageFileName, outputExt); } } BitmapEncoder bitampEncoder = this.GetBitmapEncoder(outputExt); // The image parameters... Rect drawingBounds = drawing.Bounds; int pixelWidth = (int)drawingBounds.Width; int pixelHeight = (int)drawingBounds.Height; double dpiX = 96; double dpiY = 96; // The Visual to use as the source of the RenderTargetBitmap. DrawingVisualEx drawingVisual = new DrawingVisualEx(); DrawingContext drawingContext = drawingVisual.RenderOpen(); drawingContext.DrawDrawing(drawing); drawingContext.Close(); // The BitmapSource that is rendered with a Visual. RenderTargetBitmap targetBitmap = new RenderTargetBitmap( pixelWidth, pixelHeight, dpiX, dpiY, PixelFormats.Pbgra32); targetBitmap.Render(drawingVisual); // Encoding the RenderBitmapTarget as an image file. bitampEncoder.Frames.Add(BitmapFrame.Create(targetBitmap)); using (FileStream stream = File.Create(outputFileName)) { bitampEncoder.Save(stream); } return(true); }
/// <summary> /// /// </summary> /// <returns></returns> public DrawingVisualEx GetVisual() { DrawingVisualEx visual = new DrawingVisualEx(this); using (DrawingContext drawingContext = visual.RenderOpen()) { drawingContext.DrawLine(new Pen(IsSelected ? CanvasParam.PenSelectedColor : CanvasParam.PenColor, CanvasParam.Thikness / CanvasParam.Scale), new Point(X1, CanvasParam.CanvasHeight - Y1), new Point(X2, CanvasParam.CanvasHeight - Y2)); } visual.Transform = new ScaleTransform(CanvasParam.Scale, CanvasParam.Scale, 0, CanvasParam.CanvasHeight); return(visual); }