public override void ResizeImage(Graphics graphics, Image original) { Rectangle target = RectangleResizing.Center( Rectangle.Round(graphics.VisibleClipBounds), Rectangle.Round(original.GetBounds(ref GraphicsUnit))); graphics.DrawImage(original, target); }
public byte[] imageToByteArray(System.Drawing.Image imageIn) { var o = System.Drawing.GraphicsUnit.Pixel; RectangleF r1 = imageIn.GetBounds(ref o); Rectangle r2 = new Rectangle((int)r1.X, (int)r1.Y, (int)r1.Width, (int)r1.Height); System.Drawing.Imaging.BitmapData omg = ((Bitmap)imageIn).LockBits(r2, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); byte[] rgbValues = new byte[r2.Width * r2.Height * 4]; Marshal.Copy((IntPtr)omg.Scan0, rgbValues, 0, rgbValues.Length); ((Bitmap)imageIn).UnlockBits(omg); return(rgbValues); }
static void ComposeText(StringBuilder stb, string rawtext, MathML.Rendering.WinGraphicsRenderer _mmlRendering, Graphics gr) { if (stb.Length == 0) { stb.Append(textheader); } int currpos = 0; for (; ;) { int startidx = rawtext.IndexOf("<math>", currpos); if (startidx < 0) { break; } int endidx = rawtext.IndexOf("</math>", startidx); if (endidx < 0) { break; } endidx += "</math>".Length; // all text from currpos to startidx-1 can be copyied to the stringbuilder stb.Append(rawtext, currpos, startidx - currpos); // all text from startidx to endidx-1 must be loaded into the control and rendered System.IO.StringReader rd = new StringReader(rawtext.Substring(startidx, endidx - startidx)); MathML.MathMLDocument doc = new MathML.MathMLDocument(); doc.Load(rd); rd.Close(); _mmlRendering.SetMathElement((MathML.MathMLMathElement)doc.DocumentElement); System.Drawing.Image mf = _mmlRendering.GetImage(typeof(Bitmap), gr); GraphicsUnit unit = GraphicsUnit.Point; RectangleF rect = mf.GetBounds(ref unit); string imagetext = GetRtfImage(mf); stb.Append(imageheader); stb.Append(@"\picwgoal" + Math.Ceiling(15 * rect.Width).ToString()); stb.Append(@"\pichgoal" + Math.Ceiling(15 * rect.Height).ToString()); stb.Append(" "); stb.Append(imagetext); stb.Append(imagetrailer); currpos = endidx; } stb.Append(rawtext, currpos, rawtext.Length - currpos); }
private Rectangle BoundingBox(Image img, Matrix matrix) { GraphicsUnit gu = new GraphicsUnit(); Rectangle rImg = Rectangle.Round(img.GetBounds(ref gu)); // Transform the four points of the image, to get the resized bounding box. Point topLeft = new Point(rImg.Left, rImg.Top); Point topRight = new Point(rImg.Right, rImg.Top); Point bottomRight = new Point(rImg.Right, rImg.Bottom); Point bottomLeft = new Point(rImg.Left, rImg.Bottom); Point[] points = { topLeft, topRight, bottomRight, bottomLeft }; GraphicsPath gp = new GraphicsPath(points, new[] { (byte) PathPointType.Start, (byte) PathPointType.Line, (byte) PathPointType.Line, (byte) PathPointType.Line }); gp.Transform(matrix); return Rectangle.Round(gp.GetBounds()); }
private static Bitmap ColorNonRegionFormArea(IntPtr hWnd, Image capture, Color color) { Bitmap finalCapture; using (Region region = GetRegionByHWnd(hWnd)) using (Graphics drawGraphics = Graphics.FromImage(capture)) using (SolidBrush brush = new SolidBrush(color)) { RectangleF bounds = region.GetBounds(drawGraphics); if (bounds == RectangleF.Empty) { GraphicsUnit unit = GraphicsUnit.Pixel; bounds = capture.GetBounds(ref unit); if ((GetWindowLongA(hWnd, GWL_STYLE) & TARGETWINDOW) == TARGETWINDOW) { IntPtr windowRegion = CreateRoundRectRgn(0, 0, (int) bounds.Width + 1, (int) bounds.Height + 1, 9, 9); Region r = Region.FromHrgn(windowRegion); r.Complement(bounds); drawGraphics.FillRegion(brush, r); } } else { region.Complement(bounds); drawGraphics.FillRegion(brush, region); } finalCapture = new Bitmap((int) bounds.Width, (int) bounds.Height); using (Graphics finalGraphics = Graphics.FromImage(finalCapture)) { finalGraphics.SmoothingMode = SmoothingMode.AntiAlias; finalGraphics.DrawImage(capture, new RectangleF(new PointF(0, 0), finalCapture.Size), bounds, GraphicsUnit.Pixel); } } return finalCapture; }
private void CreateImage() { if (image != null) image.Dispose(); try { // Check if image dimensions are valid Graphics infoGfx = diagramPresenter.Diagram.DisplayService.InfoGraphics; int imgWidth = (int)Math.Round((dpi / infoGfx.DpiX) * diagramPresenter.Diagram.Width); int imgHeight = (int)Math.Round((dpi / infoGfx.DpiY) * diagramPresenter.Diagram.Height); if (Math.Min(imgWidth, imgHeight) <= 0 || Math.Max(imgWidth, imgHeight) > 16000) { string msgTxt = string.Format("The selected resolution would result in a {0}x{1} pixels bitmap which is not supported.", imgWidth, imgHeight); MessageBox.Show(this, msgTxt, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { Cursor = Cursors.WaitCursor; try { image = diagramPresenter.Diagram.CreateImage(imageFormat, shapes, margin, withBackgroundCheckBox.Checked, backgroundColor, dpi); if (image != null) { GraphicsUnit unit = GraphicsUnit.Display; imageBounds = Rectangle.Round(image.GetBounds(ref unit)); } } catch (Exception exc) { MessageBox.Show(this, exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { Cursor = Cursors.Default; } } } catch (Exception exc) { MessageBox.Show(this, string.Format("Error while creating image: {0}", exc.Message), "Error while creating image", MessageBoxButtons.OK, MessageBoxIcon.Error); image = new Bitmap(1, 1); } }
private static Rectangle BoundingBox(Image img, Matrix matrix) { GraphicsUnit graphicsUnit = new GraphicsUnit(); Rectangle rectangle = Rectangle.Round(img.GetBounds(ref graphicsUnit)); // Transform the four points of the image to get the resized bounding box. Point topLeft = new Point(rectangle.Left, rectangle.Top); Point topRight = new Point(rectangle.Right, rectangle.Top); Point bottomRight = new Point(rectangle.Right, rectangle.Bottom); Point bottomLeft = new Point(rectangle.Left, rectangle.Bottom); Point[] points = new[] { topLeft, topRight, bottomRight, bottomLeft }; var types = new[] { (byte)PathPointType.Start, (byte)PathPointType.Line, (byte)PathPointType.Line, (byte)PathPointType.Line }; using (GraphicsPath graphicsPath = new GraphicsPath(points, types)) { graphicsPath.Transform(matrix); return Rectangle.Round(graphicsPath.GetBounds()); } }
private static Rectangle BoundingBox(Image img, Matrix matrix) { GraphicsUnit gu = new GraphicsUnit(); Rectangle rImg = Rectangle.Round(img.GetBounds(ref gu)); Point topLeft = new Point(rImg.Left, rImg.Top); Point topRight = new Point(rImg.Right, rImg.Top); Point bottomRight = new Point(rImg.Right, rImg.Bottom); Point bottomLeft = new Point(rImg.Left, rImg.Bottom); Point[] points = new Point[] { topLeft, topRight, bottomRight, bottomLeft }; GraphicsPath gp = new GraphicsPath(points, new byte[] { (byte)PathPointType.Start, (byte)PathPointType.Line, (byte)PathPointType.Line, (byte)PathPointType.Line }); gp.Transform(matrix); return Rectangle.Round(gp.GetBounds()); }
public bool Load(Image image) { PixelFormat pxFormat = image.PixelFormat; GraphicsUnit unit = GraphicsUnit.Pixel; RectangleF boundsF = image.GetBounds(ref unit); Rectangle bounds = new Rectangle( (int)boundsF.X, (int)boundsF.Y, (int)boundsF.Width, (int)boundsF.Height); BitmapData bitmapData = (image as Bitmap).LockBits(bounds, ImageLockMode.ReadWrite, pxFormat); try { int width = image.Width; int height = image.Height; int length = width * height; InitializeAndAllocateMemory(width, height); byte[] dst = (byte[])_data; int realWidth = (int)Math.Abs(bitmapData.Stride); int bytenum = realWidth / width; int reserved = realWidth - bytenum * width; byte* src = (byte*)bitmapData.Scan0.ToPointer(); { int index = 0; switch (bytenum) { case 1: for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++, index++, src++) { dst[index] = (byte)(*(src)); } src += reserved; } break; case 3: for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++, index++, src += 3) { dst[index] = (byte)((*(src) * 29 + *(src + 1) * 150 + *(src + 2) * 77 + 128) / 256); } src += reserved; } break; case 4: for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++, index++, src += 4) { dst[index] = (byte)((*(src) * 29 + *(src + 1) * 150 + *(src + 2) * 77 + 128) / 256); } src += reserved; } break; } } } catch { } finally { if (image != null && bitmapData != null) { (image as Bitmap).UnlockBits(bitmapData); } } return true; }
public static Image OverlayImage(Image baseImage, Image overlay) { Bitmap i = new Bitmap (baseImage.Width, baseImage.Height); using (Graphics g = Graphics.FromImage (i)) { g.DrawImage (baseImage, Point.Empty); GraphicsUnit gu = GraphicsUnit.Pixel; g.DrawImage (overlay, new RectangleF (i.Width - overlay.Width, i.Height - overlay.Height, overlay.Width, overlay.Height), overlay.GetBounds (ref gu), GraphicsUnit.Pixel); } return i; }
private void refreshImageSelection() { var s = (string)listImages.SelectedItem; if (s == null) { previewImage = null; picPreview.Invalidate(); return; } previewImage = images[s]; var units = GraphicsUnit.Pixel; imageBounds = previewImage.GetBounds(ref units); updatePreviewSize(); picPreview.Invalidate(); }
internal static void DrawImageDisabled(Graphics graphics, Image image, Rectangle imageBounds, Color background, bool unscaledImage) { if (graphics == null) { throw new ArgumentNullException("graphics"); } if (image == null) { throw new ArgumentNullException("image"); } #if GRAYSCALE_DISABLED Size imageSize = image.Size; if (disabledImageAttr == null) { // This is how I came up with this somewhat random ColorMatrix. // Its set to resemble Office10 commandbars, but still be able to // deal with hi-color (256+) icons and images. // // The idea is to scale everything down (more than just a grayscale does, // therefore the small numbers in the scaling part of matrix) // White -> some shade of gray & // Black -> Black // // Second part of the matrix is to translate everything, so all colors are // a bit brigher. // Grays become lighter and washed out looking // Black becomes a shade of gray as well. // // btw, if you do come up with something better let me know - [....] float[][] array = new float[5][]; array[0] = new float[5] {0.2125f, 0.2125f, 0.2125f, 0, 0}; array[1] = new float[5] {0.2577f, 0.2577f, 0.2577f, 0, 0}; array[2] = new float[5] {0.0361f, 0.0361f, 0.0361f, 0, 0}; array[3] = new float[5] {0, 0, 0, 1, 0}; array[4] = new float[5] {0.38f, 0.38f, 0.38f, 0, 1}; ColorMatrix grayMatrix = new ColorMatrix(array); disabledImageAttr = new ImageAttributes(); disabledImageAttr.ClearColorKey(); disabledImageAttr.SetColorMatrix(grayMatrix); } if (unscaledImage) { using (Bitmap bmp = new Bitmap(image.Width, image.Height)) { using (Graphics g = Graphics.FromImage(bmp)) { g.DrawImage(image, new Rectangle(0, 0, imageSize.Width, imageSize.Height), 0, 0, imageSize.Width, imageSize.Height, GraphicsUnit.Pixel, disabledImageAttr); } graphics.DrawImageUnscaled(bmp, imageBounds); } } else { graphics.DrawImage(image, imageBounds, 0, 0, imageSize.Width, imageSize.Height, GraphicsUnit.Pixel, disabledImageAttr); } #else // This is remarkably simple -- make a monochrome version of the image, draw once // with the button highlight color, then a second time offset by one pixel // and in the button shadow color. // Technique borrowed from comctl Toolbar. Bitmap bitmap; bool disposeBitmap = false; if (image is Bitmap) bitmap = (Bitmap) image; else { // #37659 -- metafiles can have extremely high resolutions, // so if we naively turn them into bitmaps, the performance will be very poor. // bitmap = new Bitmap(image); GraphicsUnit units = GraphicsUnit.Display; RectangleF bounds = image.GetBounds(ref units); bitmap = new Bitmap((int) (bounds.Width * graphics.DpiX / image.HorizontalResolution), (int) (bounds.Height * graphics.DpiY / image.VerticalResolution)); Graphics bitmapGraphics = Graphics.FromImage(bitmap); bitmapGraphics.Clear(Color.Transparent); bitmapGraphics.DrawImage(image, 0, 0, image.Size.Width, image.Size.Height); bitmapGraphics.Dispose(); disposeBitmap = true; } Color highlight = ControlPaint.LightLight(background); Bitmap monochrome = MakeMonochrome(bitmap, highlight); graphics.DrawImage(monochrome, new Rectangle(imageBounds.X + 1, imageBounds.Y + 1, imageBounds.Width, imageBounds.Height)); monochrome.Dispose(); Color shadow = ControlPaint.Dark(background); monochrome = MakeMonochrome(bitmap, shadow); graphics.DrawImage(monochrome, imageBounds); monochrome.Dispose(); if (disposeBitmap) bitmap.Dispose(); #endif }