/// <summary> /// See base docs. /// </summary> /// <param name="imageFormat"></param> /// <returns></returns> public override byte[] GetImageBytes(VrsDrawing.ImageFormat imageFormat) { using (var memoryStream = new MemoryStream()) { GdiPlusLock.EnforceSingleThread(() => { NativeImage.Save( memoryStream, Convert.ToSystemDrawingImageFormat(imageFormat) ); }); return(memoryStream.ToArray()); } }
/// <summary> /// See base docs. /// </summary> /// <param name="pen"></param> /// <param name="fromX"></param> /// <param name="fromY"></param> /// <param name="toX"></param> /// <param name="toY"></param> public override void DrawLine(VrsDrawing.IPen pen, int fromX, int fromY, int toX, int toY) { if (pen is PenWrapper penWrapper) { GdiPlusLock.EnforceSingleThread(() => { NativeDrawingContext.DrawLine( penWrapper.NativePen, fromX, fromY, toX, toY ); }); } }
/// <summary> /// See interface docs. /// </summary> /// <param name="red"></param> /// <param name="green"></param> /// <param name="blue"></param> /// <param name="alpha"></param> /// <param name="isCached"></param> /// <returns></returns> protected override VrsDrawing.IBrush CreateBrushWrapper(int red, int green, int blue, int alpha, bool isCached) { VrsDrawing.IBrush result = null; GdiPlusLock.EnforceSingleThread(() => { result = new BrushWrapper( new SolidBrush(Color.FromArgb(alpha, red, green, blue)), isCached ); }); return(result); }
/// <summary> /// See interface docs. /// </summary> /// <param name="red"></param> /// <param name="green"></param> /// <param name="blue"></param> /// <param name="alpha"></param> /// <param name="strokeWidth"></param> /// <param name="isCached"></param> /// <returns></returns> protected override VrsDrawing.IPen CreatePenWrapper(int red, int green, int blue, int alpha, float strokeWidth, bool isCached) { VrsDrawing.IPen result = null; GdiPlusLock.EnforceSingleThread(() => { result = new PenWrapper( new Pen( Color.FromArgb(alpha, red, green, blue), strokeWidth ), isCached ); }); return(result); }
/// <summary> /// See interface docs. /// </summary> /// <param name="pixelWidth"></param> /// <param name="pixelHeight"></param> /// <param name="backgroundBrush"></param> /// <returns></returns> public VrsDrawing.IImage Create(int pixelWidth, int pixelHeight, VrsDrawing.IBrush backgroundBrush) { var image = new Bitmap( pixelWidth, pixelHeight, PixelFormat.Format32bppArgb ); GdiPlusLock.EnforceSingleThread(() => { using (var drawing = new Drawing(Graphics.FromImage(image))) { drawing.FillRectangle(backgroundBrush, 0, 0, pixelWidth, pixelHeight); } }); return(new ImageWrapper(image)); }
/// <summary> /// See interface docs. /// </summary> /// <param name="source"></param> /// <param name="drawAction"></param> /// <returns></returns> public VrsDrawing.IImage CloneAndDraw(VrsDrawing.IImage source, Action <VrsDrawing.IDrawing> drawAction) { VrsDrawing.IImage result = null; if (source is ImageWrapper sourceWrapper) { GdiPlusLock.EnforceSingleThread(() => { result = sourceWrapper.Clone(); var nativeClone = ((ImageWrapper)result).NativeImage; using (var drawing = new Drawing(Graphics.FromImage(nativeClone))) { drawAction(drawing); } }); } return(result); }
/// <summary> /// See base docs. /// </summary> /// <param name="degrees"></param> /// <returns></returns> public override VrsDrawing.IImage Rotate(float degrees) { var result = new Bitmap(NativeImage.Width, NativeImage.Height, PixelFormat.Format32bppArgb); GdiPlusLock.EnforceSingleThread(() => { using (var graphics = Graphics.FromImage(result)) { var centreX = ((float)NativeImage.Width) / 2F; var centreY = ((float)NativeImage.Height) / 2F; graphics.TranslateTransform(centreX, centreY); graphics.RotateTransform(degrees); graphics.DrawImage( NativeImage, -centreX, -centreY, NativeImage.Width, NativeImage.Height ); } }); return(new ImageWrapper(result)); }
/// <summary> /// See base docs. /// </summary> /// <param name="width"></param> /// <param name="height"></param> /// <param name="mode"></param> /// <param name="zoomBackground"></param> /// <param name="preferSpeedOverQuality"></param> /// <returns></returns> public override VrsDrawing.IImage Resize(int width, int height, VrsDrawing.ResizeMode mode = VrsDrawing.ResizeMode.Normal, VrsDrawing.IBrush zoomBackground = null, bool preferSpeedOverQuality = true) { var result = new Bitmap(width, height, PixelFormat.Format32bppArgb); var zoomBackgroundBrushWrapper = zoomBackground as BrushWrapper; GdiPlusLock.EnforceSingleThread(() => { using (var graphics = Graphics.FromImage(result)) { graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.InterpolationMode = preferSpeedOverQuality ? InterpolationMode.Bicubic : InterpolationMode.HighQualityBicubic; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; var newWidth = width; var newHeight = height; var left = 0; var top = 0; if (mode != VrsDrawing.ResizeMode.Stretch) { var widthPercent = (double)newWidth / (double)NativeImage.Width; var heightPercent = (double)newHeight / (double)NativeImage.Height; switch (mode) { case VrsDrawing.ResizeMode.Zoom: // Resize the longest side by the smallest percentage graphics.FillRectangle(zoomBackgroundBrushWrapper.NativeBrush, 0, 0, result.Width, result.Height); if (widthPercent > heightPercent) { newWidth = Math.Min(newWidth, (int)(((double)NativeImage.Width * heightPercent) + 0.5)); } else if (heightPercent > widthPercent) { newHeight = Math.Min(newHeight, (int)(((double)NativeImage.Height * widthPercent) + 0.5)); } break; case VrsDrawing.ResizeMode.Normal: case VrsDrawing.ResizeMode.Centre: // Resize the smallest side by the largest percentage if (widthPercent > heightPercent) { newHeight = Math.Max(newHeight, (int)(((double)NativeImage.Height * widthPercent) + 0.5)); } else if (heightPercent > widthPercent) { newWidth = Math.Max(newWidth, (int)(((double)NativeImage.Width * heightPercent) + 0.5)); } break; } if (mode != VrsDrawing.ResizeMode.Normal) { left = (width - newWidth) / 2; top = (height - newHeight) / 2; } } graphics.DrawImage(NativeImage, left, top, newWidth, newHeight); } }); return(new ImageWrapper(result)); }