//Uses pointers to scan through the bitmap a LOT faster //Make sure to check "Allow unsafe code" in the project properties public static unsafe Region getRegionFast(Bitmap bitmap, Color transparencyKey, int tolerance) { //Bounds GraphicsUnit unit = GraphicsUnit.Pixel; RectangleF boundsF = bitmap.GetBounds(ref unit); Rectangle bounds = new Rectangle((int)boundsF.Left, (int)boundsF.Top, (int)boundsF.Width, (int)boundsF.Height); int yMax = (int)boundsF.Height; int xMax = (int)boundsF.Width; //Transparency Color if (tolerance <= 0) tolerance = 1; //Lock Image BitmapData bitmapData = bitmap.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); uint* pixelPtr = (uint*)bitmapData.Scan0.ToPointer(); //Stores all the rectangles for the region GraphicsPath path = new GraphicsPath(); //Scan the image, looking for lines that are NOT the transperancy color for (int y = 0; y < yMax; y++) { byte* basePos = (byte*)pixelPtr; for (int x = 0; x < xMax; x++, pixelPtr++) { //Go on with the loop if its transperancy color if (colorsMatch(pixelPtr, transparencyKey, tolerance)) continue; //Line start int x0 = x; //Find the next transparency colored pixel while (x < xMax && !colorsMatch(pixelPtr, transparencyKey, tolerance)) { x++; pixelPtr++; } //Add the line as a rectangle path.AddRectangle(new Rectangle(x0, y, x - x0, 1)); } //Go to next line pixelPtr = (uint*)(basePos + bitmapData.Stride); } //Create the Region Region outputRegion = new Region(path); //Clean Up path.Dispose(); bitmap.UnlockBits(bitmapData); return outputRegion; }
// creates a new "DRAFT" sample image private Bitmap CreateDraftImage() { string sText = "DRAFT"; Bitmap destination = new Bitmap(400, 400); using (Graphics g = Graphics.FromImage(destination)) { GraphicsUnit units = GraphicsUnit.Pixel; g.Clear(Color.White); StringFormat stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Center; stringFormat.LineAlignment = StringAlignment.Center; g.DrawString(sText, new Font( "Arial", 90, FontStyle.Bold, GraphicsUnit.Pixel), new SolidBrush( Color.FromArgb(64, Color.Black)), destination.GetBounds(ref units), stringFormat); } return destination; }
public void Bitmap1bitFeatures () { string sInFile = getInFile ("bitmaps/1bit.png"); using (Bitmap bmp = new Bitmap (sInFile)) { GraphicsUnit unit = GraphicsUnit.World; RectangleF rect = bmp.GetBounds (ref unit); Assert.AreEqual (PixelFormat.Format1bppIndexed, bmp.PixelFormat); Assert.AreEqual (0, bmp.Palette.Flags, "Palette.Flags"); Assert.AreEqual (2, bmp.Palette.Entries.Length, "Palette.Entries"); Assert.AreEqual (-16777216, bmp.Palette.Entries[0].ToArgb (), "Palette.0"); Assert.AreEqual (-1, bmp.Palette.Entries[1].ToArgb (), "Palette.1"); Assert.AreEqual (288, bmp.Width, "bmp.Width"); Assert.AreEqual (384, bmp.Height, "bmp.Height"); Assert.AreEqual (0, rect.X, "rect.X"); Assert.AreEqual (0, rect.Y, "rect.Y"); Assert.AreEqual (288, rect.Width, "rect.Width"); Assert.AreEqual (384, rect.Height, "rect.Height"); Assert.AreEqual (288, bmp.Size.Width, "bmp.Size.Width"); Assert.AreEqual (384, bmp.Size.Height, "bmp.Size.Height"); } }
/// <summary> /// Create an instance from an existing bitmap /// </summary> /// <param name="bitmap">The bitmap</param> public UnsafeBitmap(Bitmap bitmap) { this.bitmap = bitmap; GraphicsUnit unit = GraphicsUnit.Pixel; RectangleF bounds = bitmap.GetBounds(ref unit); size = new Point((int)bounds.Width, (int)bounds.Height); }
public Object(Bitmap i, string n, Point location) { image = i; name = n; GraphicsUnit units = GraphicsUnit.Point; RectangleF boundingBox = image.GetBounds(ref units); physics = new Physics(location, boundingBox); }
public static Image drawImageFromHex(char hex) { Image img = new Bitmap(20, 20); using (Graphics gfx = Graphics.FromImage(img)) { GraphicsUnit gfu = GraphicsUnit.Pixel; gfx.FillRectangle(new SolidBrush(Color.Red), img.GetBounds(ref gfu)); gfx.DrawString(hex.ToString(), new Font("Arial", 10), new SolidBrush(Color.Black), Point.Empty); } return img; }
public static Icon SmallQuestionIcon() { Bitmap bitmap = new Bitmap(16, 16); Graphics g = Graphics.FromImage(bitmap); GraphicsUnit unit = GraphicsUnit.Pixel; g.FillRectangle(Brushes.White, bitmap.GetBounds(ref unit)); g.DrawString("?",new Font("Arial",9),Brushes.Black,new PointF(2,2)); g.Flush(); return Make(Image.FromHbitmap(bitmap.GetHbitmap()), 16, true); }
public BitmapWidget (System.Drawing.Bitmap b) { this.b = b; GraphicsUnit unit = GraphicsUnit.Pixel; // // Quick hack to make stuff not too large // RectangleF bounds = b.GetBounds (ref unit); bounds.Height = System.Math.Min (bounds.Height, 600); bounds.Width = System.Math.Min (bounds.Width, 400); SetSizeRequest ((int) bounds.Width, (int) bounds.Height); }
public BitmapWidget(System.Drawing.Bitmap b) { this.b = b; GraphicsUnit unit = GraphicsUnit.Pixel; // // Quick hack to make stuff not too large // RectangleF bounds = b.GetBounds(ref unit); bounds.Height = System.Math.Min(bounds.Height, 600); bounds.Width = System.Math.Min(bounds.Width, 400); SetSizeRequest((int)bounds.Width, (int)bounds.Height); }
/* Checks bitmap features on a know 1bbp bitmap */ /* Checks bitmap features on a know 1bbp bitmap */ private void Bitmap8bitsFeatures (string filename) { using (Bitmap bmp = new Bitmap (filename)) { GraphicsUnit unit = GraphicsUnit.World; RectangleF rect = bmp.GetBounds (ref unit); Assert.AreEqual (PixelFormat.Format8bppIndexed, bmp.PixelFormat); Assert.AreEqual (110, bmp.Width, "bmp.Width"); Assert.AreEqual (100, bmp.Height, "bmp.Height"); Assert.AreEqual (0, rect.X, "rect.X"); Assert.AreEqual (0, rect.Y, "rect.Y"); Assert.AreEqual (110, rect.Width, "rect.Width"); Assert.AreEqual (100, rect.Height, "rect.Height"); Assert.AreEqual (110, bmp.Size.Width, "bmp.Size.Width"); Assert.AreEqual (100, bmp.Size.Height, "bmp.Size.Height"); } }
public void Bitmap1bitFeatures () { string sInFile = getInFile ("bitmaps/almogaver1bit.bmp"); using (Bitmap bmp = new Bitmap (sInFile)) { GraphicsUnit unit = GraphicsUnit.World; RectangleF rect = bmp.GetBounds (ref unit); // ??? why is it a 4bbp ? Assert.AreEqual (PixelFormat.Format4bppIndexed, bmp.PixelFormat); Assert.AreEqual (173, bmp.Width, "bmp.Width"); Assert.AreEqual (183, bmp.Height, "bmp.Height"); Assert.AreEqual (0, rect.X, "rect.X"); Assert.AreEqual (0, rect.Y, "rect.Y"); Assert.AreEqual (173, rect.Width, "rect.Width"); Assert.AreEqual (183, rect.Height, "rect.Height"); Assert.AreEqual (173, bmp.Size.Width, "bmp.Size.Width"); Assert.AreEqual (183, bmp.Size.Height, "bmp.Size.Height"); } }
public void Bitmap8bbpIndexedGreyscaleFeatures () { string sInFile = getInFile ("bitmaps/nature-greyscale.jpg"); using (Bitmap bmp = new Bitmap (sInFile)) { GraphicsUnit unit = GraphicsUnit.World; RectangleF rect = bmp.GetBounds (ref unit); Assert.AreEqual (PixelFormat.Format8bppIndexed, bmp.PixelFormat, "PixelFormat"); Assert.AreEqual (110, bmp.Width, "bmp.Width"); Assert.AreEqual (100, bmp.Height, "bmp.Height"); Assert.AreEqual (0, rect.X, "rect.X"); Assert.AreEqual (0, rect.Y, "rect.Y"); Assert.AreEqual (110, rect.Width, "rect.Width"); Assert.AreEqual (100, rect.Height, "rect.Height"); Assert.AreEqual (110, bmp.Size.Width, "bmp.Size.Width"); Assert.AreEqual (100, bmp.Size.Height, "bmp.Size.Height"); Assert.AreEqual (110, bmp.PhysicalDimension.Width, "bmp.PhysicalDimension.Width"); Assert.AreEqual (100, bmp.PhysicalDimension.Height, "bmp.PhysicalDimension.Height"); Assert.AreEqual (72, bmp.HorizontalResolution, "HorizontalResolution"); Assert.AreEqual (72, bmp.VerticalResolution, "VerticalResolution"); Assert.AreEqual (77896, bmp.Flags, "Flags"); ColorPalette cp = bmp.Palette; Assert.AreEqual (256, cp.Entries.Length, "Palette.Entries"); Assert.AreEqual (0, cp.Flags, "Palette.Flags"); for (int i = 0; i < 256; i++) { Color c = cp.Entries [i]; Assert.AreEqual (0xFF, c.A, "A" + i.ToString ()); Assert.AreEqual (i, c.R, "R" + i.ToString ()); Assert.AreEqual (i, c.G, "G" + i.ToString ()); Assert.AreEqual (i, c.B, "B" + i.ToString ()); } } }
void LockBitmap() { GraphicsUnit unit = GraphicsUnit.Pixel; RectangleF boundsF = bitmap.GetBounds(ref unit); Rectangle bounds = new Rectangle((int)boundsF.X, (int)boundsF.Y, (int)boundsF.Width, (int)boundsF.Height); // Figure out the number of bytes in a row // This is rounded up to be a multiple of 4 // bytes, since a scan line in an image must always be a multiple of 4 bytes // in length. int pixelsize; switch (bitmap.PixelFormat) { case PixelFormat.Format24bppRgb: pixelsize = 3; break; case PixelFormat.Format32bppArgb: pixelsize = 4; break; default: throw new NotSupportedException(); } width = (int)boundsF.Width * pixelsize; if (width % 4 != 0) { width = 4 * (width / 4 + 1); } bitmapData = bitmap.LockBits(bounds, ImageLockMode.ReadWrite, bitmap.PixelFormat); pBase = (byte *)bitmapData.Scan0.ToPointer(); }
public static unsafe Region getRegionFast(Bitmap bitmap, Color transparencyKey, int tolerance) { GraphicsUnit unit = GraphicsUnit.Pixel; RectangleF boundsF = bitmap.GetBounds(ref unit); Rectangle bounds = new Rectangle((int)boundsF.Left, (int)boundsF.Top, (int)boundsF.Width, (int)boundsF.Height); int yMax = (int)boundsF.Height; int xMax = (int)boundsF.Width; if (tolerance <= 0) tolerance = 1; BitmapData bitmapData = bitmap.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); uint* pixelPtr = (uint*)bitmapData.Scan0.ToPointer(); GraphicsPath path = new GraphicsPath(); for (int y = 0; y < yMax; y++) { byte* basePos = (byte*)pixelPtr; for (int x = 0; x < xMax; x++, pixelPtr++) { if (colorsMatch(pixelPtr, transparencyKey, tolerance)) continue; int x0 = x; while (x < xMax && !colorsMatch(pixelPtr, transparencyKey, tolerance)) { x++; pixelPtr++; } path.AddRectangle(new Rectangle(x0, y, x - x0, 1)); } pixelPtr = (uint*)(basePos + bitmapData.Stride); } Region outputRegion = new Region(path); path.Dispose(); bitmap.UnlockBits(bitmapData); return outputRegion; }
public void LockBitmap() { GraphicsUnit unit = GraphicsUnit.Pixel; RectangleF boundsF = bitmap.GetBounds(ref unit); Rectangle bounds = new Rectangle( (int)boundsF.X, (int)boundsF.Y, (int)boundsF.Width, (int)boundsF.Height ); // Figure out the number of bytes in a row // This is rounded up to be a multiple of 4 // bytes, since a scan line in an image must always be a multiple of 4 bytes // in length. _width = (int)boundsF.Width * sizeof(PixelData); if (_width % 4 != 0) { _width = 4 * (_width / 4 + 1); } _bitmapData = bitmap.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); _base = (Byte *)_bitmapData.Scan0.ToPointer(); }
private void RenderEQ(byte[] EqDataArray) { if (this.info.SupportGfxLCD) { object obj2; Monitor.Enter(obj2 = this.DWriteMutex); try { Bitmap image = new Bitmap(this.LCD_CONFIG.ColumnsGraphics, this.LCD_CONFIG.RowsGraphics); GraphicsUnit pixel = GraphicsUnit.Pixel; RectangleF bounds = image.GetBounds(ref pixel); Graphics graphics = Graphics.FromImage(image); graphics.FillRectangle(Brushes.White, bounds); for (int i = 0; i < this.EQSettings.Render_BANDS; i++) { RectangleF ef2; if (this.DoDebug) { Log.Info("LCDHypeWrapper.RenderEQ(): Rendering {0} band {1} = {2}", new object[] { this.EQSettings.UseNormalEq ? "Normal EQ" : (this.EQSettings.UseStereoEq ? "Stereo EQ" : (this.EQSettings.UseVUmeter ? "VU Meter" : "VU Meter 2")), i, this.EQSettings.UseNormalEq ? this.EQSettings.EqArray[1 + i].ToString() : (this.EQSettings.UseStereoEq ? (this.EQSettings.EqArray[1 + i].ToString() + " : " + this.EQSettings.EqArray[9 + i].ToString()) : (this.EQSettings.EqArray[1 + i].ToString() + " : " + this.EQSettings.EqArray[2 + i].ToString())) }); } if (this.EQSettings.UseNormalEq) { ef2 = new RectangleF((bounds.X + (i * (((int)bounds.Width) / this.EQSettings.Render_BANDS))) + 1f, bounds.Y + (((int)bounds.Height) - this.EQSettings.EqArray[1 + i]), (float)((((int)bounds.Width) / this.EQSettings.Render_BANDS) - 2), (float)this.EQSettings.EqArray[1 + i]); graphics.FillRectangle(Brushes.Black, ef2); } else { int num2; RectangleF ef3; if (this.EQSettings.UseStereoEq) { int num4 = (((int)bounds.Width) / 2) / this.EQSettings.Render_BANDS; num2 = i * num4; int num3 = (i + this.EQSettings.Render_BANDS) * num4; ef2 = new RectangleF((bounds.X + num2) + 1f, bounds.Y + (((int)bounds.Height) - this.EQSettings.EqArray[1 + i]), (float)(num4 - 2), (float)this.EQSettings.EqArray[1 + i]); ef3 = new RectangleF((bounds.X + num3) + 1f, bounds.Y + (((int)bounds.Height) - this.EQSettings.EqArray[9 + i]), (float)(num4 - 2), (float)this.EQSettings.EqArray[9 + i]); graphics.FillRectangle(Brushes.Black, ef2); graphics.FillRectangle(Brushes.Black, ef3); } else if (this.EQSettings.UseVUmeter | this.EQSettings.UseVUmeter2) { ef2 = new RectangleF(bounds.X + 1f, bounds.Y + 1f, (float)this.EQSettings.EqArray[1 + i], (float)(((int)(bounds.Height / 2f)) - 2)); num2 = this.EQSettings.UseVUmeter ? 0 : (((int)bounds.Width) - this.EQSettings.EqArray[2 + i]); ef3 = new RectangleF((bounds.X + num2) + 1f, (bounds.Y + (bounds.Height / 2f)) + 1f, (float)this.EQSettings.EqArray[2 + i], (float)(((int)(bounds.Height / 2f)) - 2)); graphics.FillRectangle(Brushes.Black, ef2); graphics.FillRectangle(Brushes.Black, ef3); } } } this.DrawImage(image); return; } catch (Exception exception) { Log.Info("LCDHypeWrapper.DisplayEQ(): CAUGHT EXCEPTION {0}", new object[] {exception}); if (exception.Message.Contains("ThreadAbortException")) {} return; } finally { Monitor.Exit(obj2); } } lock (this.DWriteMutex) { if (this.EQSettings.UseVUmeter || this.EQSettings.UseVUmeter2) { if (this.DoDebug) { Log.Info("LCDHypeWrapper.RenderEQ(): Drawing VU meter"); } string strLeft = ""; string strRight = ""; int segmentCount = this.LCD_CONFIG.ColumnsText; if (this.EQSettings._useVUindicators) { strLeft = "L"; strRight = "R"; segmentCount--; } if (this.DoDebug) { Log.Info("LCDHypeWrapper.RenderEQ(): segment count: {0}", segmentCount); } for (int j = 0; j < segmentCount; j++) { if (EqDataArray[1] > j) { strLeft = strLeft + '='; } else { strLeft = strLeft + ' '; } if (EqDataArray[2] > j) { strRight = strRight + '='; } else { strRight = strRight + ' '; } } if (this.EQSettings.UseVUmeter2) { char[] strArray = strRight.ToCharArray(); Array.Reverse(strArray); strRight = new string(strArray); //strRight = strRight.Replace(">", "<"); } if (this.DoDebug) { Log.Info("LCDHypeWrapper.RenderEQ(): Sending VU meter data to display: L = \"{0}\" - R = \"{1}\"", new object[] {strLeft, strRight}); } this.LCDHypeWrapper_SetLine(0, strLeft); this.LCDHypeWrapper_SetLine(1, strRight); } } }
public void Bitmap32bitsFeatures () { string sInFile = getInFile ("bitmaps/almogaver32bits.tif"); using (Bitmap bmp = new Bitmap (sInFile)) { GraphicsUnit unit = GraphicsUnit.World; RectangleF rect = bmp.GetBounds (ref unit); // MS reports 24 bpp while we report 32 bpp // Assert.AreEqual (PixelFormat.Format24bppRgb, bmp.PixelFormat); Assert.AreEqual (173, bmp.Width, "bmp.Width"); Assert.AreEqual (183, bmp.Height, "bmp.Height"); Assert.AreEqual (0, rect.X, "rect.X"); Assert.AreEqual (0, rect.Y, "rect.Y"); Assert.AreEqual (173, rect.Width, "rect.Width"); Assert.AreEqual (183, rect.Height, "rect.Height"); Assert.AreEqual (173, bmp.Size.Width, "bmp.Size.Width"); Assert.AreEqual (183, bmp.Size.Height, "bmp.Size.Height"); } }
public static unsafe GraphicsPath CreateRegion(Bitmap bitmap) { GraphicsUnit unit = GraphicsUnit.Pixel; RectangleF boundsF = bitmap.GetBounds(ref unit); Rectangle bounds = new Rectangle((int)boundsF.Left, (int)boundsF.Top, (int)boundsF.Width, (int)boundsF.Height); Color transparencyKey; if (UseTransparencyColorTopLeft) { transparencyKey = bitmap.GetPixel(0, 0); } else { transparencyKey = Color.FromArgb(255, 255, 0, 255); } uint key = (uint)((transparencyKey.A << 24) | (transparencyKey.R << 16) | (transparencyKey.G << 8) | (transparencyKey.B << 0)); // Access raw bits of the image BitmapData bitmapData = bitmap.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); uint* pixelPtr = (uint*)bitmapData.Scan0.ToPointer(); // Get it only once int yMax = (int)boundsF.Height; int xMax = (int)boundsF.Width; // To store the rectangles GraphicsPath path = new GraphicsPath(); for (int y = 0; y < yMax; y++) { // Store pointer to get next line from it byte* basePos = (byte*)pixelPtr; for (int x = 0; x < xMax; x++, pixelPtr++) { // Is it transparent? if (*pixelPtr == key) continue; // Yes, go on with the loop // Store where scan starts int x0 = x; //not transparent - scan until we find the next transparent byte while (x < xMax && (*pixelPtr != key)) { ++x; pixelPtr++; } //add rectangle found to path path.AddRectangle(new Rectangle(x0, y, x - x0, 1)); } // Goto next line pixelPtr = (uint*)(basePos + bitmapData.Stride); } bitmap.UnlockBits(bitmapData); return path; }
public void Bitmap32bitFeatures () { string sInFile = getInFile ("bitmaps/almogaver32bits.bmp"); using (Bitmap bmp = new Bitmap (sInFile)) { GraphicsUnit unit = GraphicsUnit.World; RectangleF rect = bmp.GetBounds (ref unit); Assert.AreEqual (173, bmp.Width, "bmp.Width"); Assert.AreEqual (183, bmp.Height, "bmp.Height"); Assert.AreEqual (0, rect.X, "rect.X"); Assert.AreEqual (0, rect.Y, "rect.Y"); Assert.AreEqual (173, rect.Width, "rect.Width"); Assert.AreEqual (183, rect.Height, "rect.Height"); Assert.AreEqual (173, bmp.Size.Width, "bmp.Size.Width"); Assert.AreEqual (183, bmp.Size.Height, "bmp.Size.Height"); } }
public void NonInvertedBitmap () { // regression check against http://bugzilla.ximian.com/show_bug.cgi?id=80751 string sInFile = getInFile ("bitmaps/non-inverted.bmp"); using (Bitmap bmp = new Bitmap (sInFile)) { GraphicsUnit unit = GraphicsUnit.World; RectangleF rect = bmp.GetBounds (ref unit); Assert.AreEqual (90, bmp.Width, "bmp.Width"); Assert.AreEqual (60, bmp.Height, "bmp.Height"); Assert.AreEqual (0, rect.X, "rect.X"); Assert.AreEqual (0, rect.Y, "rect.Y"); Assert.AreEqual (90, rect.Width, "rect.Width"); Assert.AreEqual (60, rect.Height, "rect.Height"); Assert.AreEqual (90, bmp.Size.Width, "bmp.Size.Width"); Assert.AreEqual (60, bmp.Size.Height, "bmp.Size.Height"); // sampling values from a well known bitmap Assert.AreEqual (-16777216, bmp.GetPixel (12, 21).ToArgb (), "12,21"); Assert.AreEqual (-1, bmp.GetPixel (21, 37).ToArgb (), "21,37"); } }
private void LockBitmap(Bitmap bitmap) { GraphicsUnit unit = GraphicsUnit.Pixel; RectangleF boundsF = bitmap.GetBounds(ref unit); Rectangle bounds = new Rectangle((int)boundsF.X, (int)boundsF.Y, (int)boundsF.Width, (int)boundsF.Height); // Figure out the number of bytes in a row // This is rounded up to be a multiple of 4 // bytes, since a scan line in an image must always be a multiple of 4 bytes // in length. scanWidth = (int)boundsF.Width * sizeof(PixelData); if (scanWidth % 4 != 0) { scanWidth = 4 * (scanWidth / 4 + 1); } bitmapData = bitmap.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); pBase = (byte*)bitmapData.Scan0.ToPointer(); }
private void PaintImage(Bitmap image, object sender, PaintEventArgs e, string drawString) { Graphics g = e.Graphics; PictureBox pictureBox = sender as PictureBox; if (image != null) { GraphicsUnit Unit = GraphicsUnit.Pixel; RectangleF bounds = image.GetBounds(ref Unit); float Scale = ComputeScale(); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; RectangleF destRect = new RectangleF(); destRect.Size = new SizeF(bounds.Width * Scale, bounds.Height * Scale); destRect.Location = new PointF(-ImageOffsetX * Scale, -ImageOffsetY * Scale); g.DrawImage(image, destRect, bounds, Unit); } Font drawFont = new Font("Arial", 12); SizeF textSize = g.MeasureString(drawString, drawFont); DrawOutlinedString(g, (int)(pictureBox.Width / 2 - textSize.Width / 2), (int)(pictureBox.Height - textSize.Height), drawString, drawFont); }
public void Bitmap96Features () { string sInFile = getInFile ("bitmaps/96x96x256.ico"); using (Bitmap bmp = new Bitmap (sInFile)) { GraphicsUnit unit = GraphicsUnit.World; RectangleF rect = bmp.GetBounds (ref unit); Assert.IsTrue (bmp.RawFormat.Equals (ImageFormat.Icon), "Icon"); Assert.AreEqual (PixelFormat.Format32bppArgb, bmp.PixelFormat); Assert.AreEqual (73746, bmp.Flags, "bmp.Flags"); Assert.AreEqual (256, bmp.Palette.Entries.Length, "Palette"); Assert.AreEqual (1, bmp.FrameDimensionsList.Length, "FrameDimensionsList"); Assert.AreEqual (0, bmp.PropertyIdList.Length, "PropertyIdList"); Assert.AreEqual (0, bmp.PropertyItems.Length, "PropertyItems"); #if NET_2_0 Assert.IsNull (bmp.Tag, "Tag"); #endif Assert.AreEqual (96.0f, bmp.HorizontalResolution, "HorizontalResolution"); Assert.AreEqual (96.0f, bmp.VerticalResolution, "VerticalResolution"); Assert.AreEqual (96, bmp.Width, "bmp.Width"); Assert.AreEqual (96, bmp.Height, "bmp.Height"); Assert.AreEqual (0, rect.X, "rect.X"); Assert.AreEqual (0, rect.Y, "rect.Y"); Assert.AreEqual (96, rect.Width, "rect.Width"); Assert.AreEqual (96, rect.Height, "rect.Height"); Assert.AreEqual (96, bmp.Size.Width, "bmp.Size.Width"); Assert.AreEqual (96, bmp.Size.Height, "bmp.Size.Height"); } }
private Point GetPixelSize(Bitmap bitmap) { GraphicsUnit unit = GraphicsUnit.Pixel; RectangleF bounds = bitmap.GetBounds(ref unit); return new Point((int)bounds.Width, (int)bounds.Height); }
public void Bitmap16Features () { string sInFile = getInFile ("bitmaps/smiley.ico"); using (Bitmap bmp = new Bitmap (sInFile)) { GraphicsUnit unit = GraphicsUnit.World; RectangleF rect = bmp.GetBounds (ref unit); Assert.IsTrue (bmp.RawFormat.Equals (ImageFormat.Icon), "Icon"); // note that image is "promoted" to 32bits Assert.AreEqual (PixelFormat.Format32bppArgb, bmp.PixelFormat); Assert.AreEqual (73746, bmp.Flags, "bmp.Flags"); Assert.AreEqual (16, bmp.Palette.Entries.Length, "Palette"); Assert.AreEqual (-16777216, bmp.Palette.Entries[0].ToArgb (), "Palette#0"); Assert.AreEqual (-16777216, bmp.Palette.Entries[1].ToArgb (), "Palette#1"); Assert.AreEqual (-16744448, bmp.Palette.Entries[2].ToArgb (), "Palette#2"); Assert.AreEqual (-8355840, bmp.Palette.Entries[3].ToArgb (), "Palette#3"); Assert.AreEqual (-16777088, bmp.Palette.Entries[4].ToArgb (), "Palette#4"); Assert.AreEqual (-8388480, bmp.Palette.Entries[5].ToArgb (), "Palette#5"); Assert.AreEqual (-16744320, bmp.Palette.Entries[6].ToArgb (), "Palette#6"); Assert.AreEqual (-4144960, bmp.Palette.Entries[7].ToArgb (), "Palette#7"); Assert.AreEqual (-8355712, bmp.Palette.Entries[8].ToArgb (), "Palette#8"); Assert.AreEqual (-65536, bmp.Palette.Entries[9].ToArgb (), "Palette#9"); Assert.AreEqual (-16711936, bmp.Palette.Entries[10].ToArgb (), "Palette#10"); Assert.AreEqual (-256, bmp.Palette.Entries[11].ToArgb (), "Palette#11"); Assert.AreEqual (-16776961, bmp.Palette.Entries[12].ToArgb (), "Palette#12"); Assert.AreEqual (-65281, bmp.Palette.Entries[13].ToArgb (), "Palette#13"); Assert.AreEqual (-16711681, bmp.Palette.Entries[14].ToArgb (), "Palette#14"); Assert.AreEqual (-1, bmp.Palette.Entries[15].ToArgb (), "Palette#15"); Assert.AreEqual (1, bmp.FrameDimensionsList.Length, "FrameDimensionsList"); Assert.AreEqual (0, bmp.PropertyIdList.Length, "PropertyIdList"); Assert.AreEqual (0, bmp.PropertyItems.Length, "PropertyItems"); #if NET_2_0 Assert.IsNull (bmp.Tag, "Tag"); #endif Assert.AreEqual (96.0f, bmp.HorizontalResolution, "HorizontalResolution"); Assert.AreEqual (96.0f, bmp.VerticalResolution, "VerticalResolution"); Assert.AreEqual (16, bmp.Width, "bmp.Width"); Assert.AreEqual (16, bmp.Height, "bmp.Height"); Assert.AreEqual (0, rect.X, "rect.X"); Assert.AreEqual (0, rect.Y, "rect.Y"); Assert.AreEqual (16, rect.Width, "rect.Width"); Assert.AreEqual (16, rect.Height, "rect.Height"); Assert.AreEqual (16, bmp.Size.Width, "bmp.Size.Width"); Assert.AreEqual (16, bmp.Size.Height, "bmp.Size.Height"); } }
public void Bitmap2bitFeatures () { string sInFile = getInFile ("bitmaps/81674-2bpp.png"); using (Bitmap bmp = new Bitmap (sInFile)) { GraphicsUnit unit = GraphicsUnit.World; RectangleF rect = bmp.GetBounds (ref unit); // quite a promotion! (2 -> 32) Assert.AreEqual (PixelFormat.Format32bppArgb, bmp.PixelFormat); // MS returns a random Flags value (not a good sign) //Assert.AreEqual (0, bmp.Palette.Flags, "Palette.Flags"); Assert.AreEqual (0, bmp.Palette.Entries.Length, "Palette.Entries"); Assert.AreEqual (100, bmp.Width, "bmp.Width"); Assert.AreEqual (100, bmp.Height, "bmp.Height"); Assert.AreEqual (0, rect.X, "rect.X"); Assert.AreEqual (0, rect.Y, "rect.Y"); Assert.AreEqual (100, rect.Width, "rect.Width"); Assert.AreEqual (100, rect.Height, "rect.Height"); Assert.AreEqual (100, bmp.Size.Width, "bmp.Size.Width"); Assert.AreEqual (100, bmp.Size.Height, "bmp.Size.Height"); } }
public void Bitmap24bitFeatures () { string sInFile = getInFile ("bitmaps/nature24bits.jpg"); using (Bitmap bmp = new Bitmap (sInFile)) { GraphicsUnit unit = GraphicsUnit.World; RectangleF rect = bmp.GetBounds (ref unit); Assert.AreEqual (PixelFormat.Format24bppRgb, bmp.PixelFormat, "PixelFormat"); Assert.AreEqual (110, bmp.Width, "bmp.Width"); Assert.AreEqual (100, bmp.Height, "bmp.Height"); Assert.AreEqual (0, rect.X, "rect.X"); Assert.AreEqual (0, rect.Y, "rect.Y"); Assert.AreEqual (110, rect.Width, "rect.Width"); Assert.AreEqual (100, rect.Height, "rect.Height"); Assert.AreEqual (110, bmp.Size.Width, "bmp.Size.Width"); Assert.AreEqual (100, bmp.Size.Height, "bmp.Size.Height"); Assert.AreEqual (110, bmp.PhysicalDimension.Width, "bmp.PhysicalDimension.Width"); Assert.AreEqual (100, bmp.PhysicalDimension.Height, "bmp.PhysicalDimension.Height"); Assert.AreEqual (72, bmp.HorizontalResolution, "HorizontalResolution"); Assert.AreEqual (72, bmp.VerticalResolution, "VerticalResolution"); Assert.AreEqual (77960, bmp.Flags, "Flags"); Assert.AreEqual (0, bmp.Palette.Entries.Length, "Palette.Entries"); /* note: under MS flags aren't constant between executions in this case (no palette) */ } }
public void Bitmap4bitFeatures () { string sInFile = getInFile ("bitmaps/4bit.png"); using (Bitmap bmp = new Bitmap (sInFile)) { GraphicsUnit unit = GraphicsUnit.World; RectangleF rect = bmp.GetBounds (ref unit); Assert.AreEqual (PixelFormat.Format4bppIndexed, bmp.PixelFormat); Assert.AreEqual (0, bmp.Palette.Flags, "Palette.Flags"); Assert.AreEqual (16, bmp.Palette.Entries.Length, "Palette.Entries"); Assert.AreEqual (-12106173, bmp.Palette.Entries[0].ToArgb (), "Palette.0"); Assert.AreEqual (-10979957, bmp.Palette.Entries[1].ToArgb (), "Palette.1"); Assert.AreEqual (-8879241, bmp.Palette.Entries[2].ToArgb (), "Palette.2"); Assert.AreEqual (-10381134, bmp.Palette.Entries[3].ToArgb (), "Palette.3"); Assert.AreEqual (-7441574, bmp.Palette.Entries[4].ToArgb (), "Palette.4"); Assert.AreEqual (-6391673, bmp.Palette.Entries[5].ToArgb (), "Palette.5"); Assert.AreEqual (-5861009, bmp.Palette.Entries[6].ToArgb (), "Palette.6"); Assert.AreEqual (-3824008, bmp.Palette.Entries[7].ToArgb (), "Palette.7"); Assert.AreEqual (-5790569, bmp.Palette.Entries[8].ToArgb (), "Palette.8"); Assert.AreEqual (-6178617, bmp.Palette.Entries[9].ToArgb (), "Palette.9"); Assert.AreEqual (-4668490, bmp.Palette.Entries[10].ToArgb (), "Palette.10"); Assert.AreEqual (-5060143, bmp.Palette.Entries[11].ToArgb (), "Palette.11"); Assert.AreEqual (-3492461, bmp.Palette.Entries[12].ToArgb (), "Palette.12"); Assert.AreEqual (-2967099, bmp.Palette.Entries[13].ToArgb (), "Palette.13"); Assert.AreEqual (-2175574, bmp.Palette.Entries[14].ToArgb (), "Palette.14"); Assert.AreEqual (-1314578, bmp.Palette.Entries[15].ToArgb (), "Palette.15"); Assert.AreEqual (288, bmp.Width, "bmp.Width"); Assert.AreEqual (384, bmp.Height, "bmp.Height"); Assert.AreEqual (0, rect.X, "rect.X"); Assert.AreEqual (0, rect.Y, "rect.Y"); Assert.AreEqual (288, rect.Width, "rect.Width"); Assert.AreEqual (384, rect.Height, "rect.Height"); Assert.AreEqual (288, bmp.Size.Width, "bmp.Size.Width"); Assert.AreEqual (384, bmp.Size.Height, "bmp.Size.Height"); } }
public FastBitmap(Bitmap bitmap) : this(bitmap, System.Drawing.Rectangle.Round(bitmap.GetBounds(ref graphicsUnit))) { }
private static void CreateAvatarWithInitials(string userId, int size, string fullname, string initials, string pathToFile) { using (var bitmap = new Bitmap(size, size, PixelFormat.Format24bppRgb)) using (var g = Graphics.FromImage(bitmap)) { g.SmoothingMode = SmoothingMode.AntiAlias; var backgroundColor = GetColorFor(userId, fullname); g.Clear(backgroundColor); var units = GraphicsUnit.Pixel; using (var rectangleFont = new Font("Arial", (int)(size / 4), FontStyle.Bold)) { g.DrawString(initials, rectangleFont, Brushes.Beige, bitmap.GetBounds(ref units), Center); } bitmap.Save(pathToFile, ImageFormat.Png); } }
//预处理,包括分割、二值化、去边缘杂点、平移居中 public List<Bitmap> Preproc(Bitmap im) { int[] dx = new int[] { 0, 0, -1, 1, 1, 1, -1, -1 }; int[] dy = new int[] { 1, -1, 0, 0, -1, 1, 1, -1 }; List<Bitmap> result = new List<Bitmap>(); for (int i = 0; i < 4; i++) { int l = i * 11 + 1; //分割 Bitmap tmp = new Bitmap(11, 24); Graphics g = Graphics.FromImage(tmp); g.DrawImage(im, 0, 0, new Rectangle(l, 0, 11, 24), GraphicsUnit.Pixel); g.Save(); g.Dispose(); //二值化 int threshold = 200; //阈值 for (int x = 0; x < tmp.Width; x++) for (int y = 0; y < tmp.Height; y++) { Color col = tmp.GetPixel(x, y); int sum = col.R + col.G + col.B; sum /= 3; if (sum <= threshold) tmp.SetPixel(x, y, Color.Black); else tmp.SetPixel(x, y, Color.White); } //去除噪点 HashSet<Point> edge = new HashSet<Point>(); //edge--边缘黑点(仅左右边缘) for (int ih = 0; ih < tmp.Height; ih++) { if (tmp.GetPixel(0, ih).ToArgb() == Color.Black.ToArgb()) { edge.Add(new Point(0, ih)); } if (tmp.GetPixel(tmp.Width - 1, ih).ToArgb() == Color.Black.ToArgb()) { edge.Add(new Point(tmp.Width - 1, ih)); } } //BFS Queue<Point> q = new Queue<Point>(); HashSet<Point> rec = new HashSet<Point>(); //rec--记录块中黑点个数 foreach (Point p1 in edge) { if (tmp.GetPixel(p1.X, p1.Y).ToArgb() == Color.White.ToArgb()) continue; rec.Clear(); q.Enqueue(p1); rec.Add(p1); while (q.Count > 0) { Point now = q.Dequeue(); for (int ii = 0; ii < 8; ii++) { var pix = GraphicsUnit.Pixel;//getbound要求一个引用。。。 Point tp = now; tp.Offset(dx[ii], dy[ii]); //(1)图像之内 //(2)是黑色 //(3)尚未被标记 if (tmp.GetBounds(ref pix).Contains(tp) && tmp.GetPixel(tp.X, tp.Y).ToArgb() == Color.Black.ToArgb() && !rec.Contains(tp)) { q.Enqueue(tp); rec.Add(tp); } } } //bfs结束,块中点少于阈值,全部染白 if (rec.Count <= 6) { foreach (Point po in rec) { tmp.SetPixel(po.X, po.Y, Color.White); } } } //平移,使图片居中 int x1 = -1, x2 = 100, y1 = -1, y2 = 100; for (int x = 0; x < tmp.Width; x++) for (int y = 0; y < tmp.Height; y++) { if (tmp.GetPixel(x, y).ToArgb() == Color.Black.ToArgb()) { x1 = Math.Max(x1, x); x2 = Math.Min(x2, x); y1 = Math.Max(y1, y); y2 = Math.Min(y2, y); } } Bitmap tmp2 = new Bitmap(11, 24); g = Graphics.FromImage(tmp2); g.FillRectangle(new SolidBrush(Color.White), 0, 0, 11, 24); g.DrawImage(tmp, 5 - (x1 + x2) / 2, 12 - (y1 + y2) / 2); g.Save(); g.Dispose(); result.Add(tmp2); //tmp.Save(@".\"+i.ToString()+".bmp"); } return result; }
private Image PreparePhoto(byte[] photo) { if (photo == null || photo.Length == 0) return null; using (var stream = new MemoryStream(photo)) using (var image = Image.FromStream(stream)) { var imageMinSize = Math.Min(image.Width, image.Height); var size = imageSize; if (imageMinSize < 96) size = 64; if (imageMinSize < 64) size = 32; using (var bitmap = new Bitmap(size, size)) using (var g = Graphics.FromImage(bitmap)) { var delta = (image.Width - image.Height) / 2; var srcRect = new RectangleF(0f, 0f, imageMinSize, imageMinSize); if (image.Width < image.Height) srcRect.Y = -delta; else srcRect.X = delta; g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.CompositingQuality = CompositingQuality.HighQuality; var gu = GraphicsUnit.Pixel; var destRect = bitmap.GetBounds(ref gu); g.DrawImage(image, destRect, srcRect, gu); var saveStream = new MemoryStream(); bitmap.Save(saveStream, ImageFormat.Png); return Image.FromStream(saveStream); } } }
private void updateDestImage() { if (this.sourceImage != null && this.preferences != null) { if (this.destinationImage != null) { this.destinationImage.Dispose(); } FrameDimension frameDimensions = new FrameDimension(this.srcImage.FrameDimensionsList[0]); int destWidth; int destHeight; if (preferences.FramesPerRow > 0) { int numRows = (int)Math.Ceiling(numFrames / (double)preferences.FramesPerRow); int numCols = (int)Math.Min(numFrames, preferences.FramesPerRow); destWidth = (numCols * (this.srcImage.Width + preferences.BetweenFrameWidth)) + preferences.BetweenFrameWidth; destHeight = numRows * (this.srcImage.Height + preferences.BetweenFrameWidth) + preferences.BetweenFrameWidth; } else { destWidth = (this.srcImage.Width + preferences.BetweenFrameWidth) * numFrames + preferences.BetweenFrameWidth; destHeight = this.srcImage.Height + 2 * preferences.BetweenFrameWidth; } this.destinationImage = new Bitmap(destWidth, destHeight); Graphics destGraphics = Graphics.FromImage(this.destinationImage); GraphicsUnit unit = GraphicsUnit.Pixel; Brush bgBrush = new SolidBrush(preferences.BetweenFrameColor); destGraphics.FillRectangle(bgBrush, destinationImage.GetBounds(ref unit)); bgBrush.Dispose(); int currentX, currentY; for (int frame = 0; frame < numFrames; frame++) { this.srcImage.SelectActiveFrame(frameDimensions, frame); if (preferences.FramesPerRow <= 0) { currentX = preferences.BetweenFrameWidth + (preferences.BetweenFrameWidth + srcImage.Width) * frame; currentY = preferences.BetweenFrameWidth; } else { int fila, columna; fila = (frame / preferences.FramesPerRow); columna = (frame % preferences.FramesPerRow); currentX = preferences.BetweenFrameWidth + (preferences.BetweenFrameWidth + srcImage.Width) * columna; currentY = preferences.BetweenFrameWidth + (preferences.BetweenFrameWidth + srcImage.Height) * fila; } Bitmap modifiedImage = getModifiedImage(); if (preferences.DrawFlipped) { modifiedImage.RotateFlip(RotateFlipType.RotateNoneFlipX); } destGraphics.DrawImageUnscaled(modifiedImage, currentX, currentY); } destGraphics.Dispose(); this.sourceImage.Width = (int)Math.Ceiling(this.srcImage.Width * preferences.Zoom); this.sourceImage.Height = (int)Math.Ceiling(this.srcImage.Height * preferences.Zoom); this.sourceImage.ZoomFactor = preferences.Zoom; this.destImage.Image = this.destinationImage; this.destImage.Width = (int)Math.Ceiling(this.destinationImage.Width * preferences.Zoom); this.destImage.Height = (int)Math.Ceiling(this.destinationImage.Height * preferences.Zoom); this.destImage.ZoomFactor = preferences.Zoom; } }