public static Bitmap AutoScale(Bitmap _currentBitmap) { Bitmap bmap = (Bitmap)_currentBitmap; LockBitmap lockBitmap = new LockBitmap(bmap); lockBitmap.LockBits(); Color c; // Find min point on X axis with non 0 data // Find max point on X axis with non 0 data // Find min point on Y axis with non 0 data // Find max point on Y axis with non 0 data int i, j; int widthMinus1 = bmap.Width - 1; int heightMinus1 = bmap.Height - 1; int xMin = widthMinus1, yMin = heightMinus1; int xMax = 0, yMax = 0; for (i = 0; i < bmap.Width; i++) { for (j = 0; j < bmap.Height; j++) { c = bmap.GetPixel(i, j); if (c.R != 0 || c.G != 0 || c.B != 0) { xMin = Math.Min(xMin, i); yMin = Math.Min(yMin, j); } } } for (i = widthMinus1; i >= 0; i--) { for (j = heightMinus1; j >= 0; j--) { c = bmap.GetPixel(i, j); if (c.R != 0 || c.G != 0 || c.B != 0) { xMax = Math.Max(xMax, i); yMax = Math.Max(yMax, j); } } } lockBitmap.UnlockBits(); return TranslateAndScaleImage(bmap, new PointF(-xMin, -yMax), new PointF(Math.Abs((float)bmap.Width / (float)(xMax - xMin)), Math.Abs((float)bmap.Height / (float)(yMax - yMin)))); // , InterpolationMode.HighQualityBicubic, bmap.PixelFormat); }
private void ReadImage() { Bitmap image = Obj; Bitmap greyScaleImage = BitmapManipulator.MakeGrayscale3(image); //Bitmap greyScaleImage = BitmapManipulator.ApplyGreyscale(image); //int i, j; GreyImage = new int[Obj.Width, Obj.Height]; //[Row,Column] LockBitmap lockBitmap = new LockBitmap(greyScaleImage); lockBitmap.LockBits(); for (int y = 0; y < lockBitmap.Height; y++) { for (int x = 0; x < lockBitmap.Width; x++) { GreyImage[x, y] = (int)lockBitmap.GetPixel(x, y).R; if (lockBitmap.GetPixel(x, y).R != lockBitmap.GetPixel(x, y).B && lockBitmap.GetPixel(x, y).R != lockBitmap.GetPixel(x, y).G) { int foo = 0; foo = foo + 1; } } } lockBitmap.UnlockBits(); //BitmapData bitmapData1 = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), // ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); //unsafe //{ // byte* imagePointer1 = (byte*)bitmapData1.Scan0; // for (i = 0; i < bitmapData1.Height; i++) // { // for (j = 0; j < bitmapData1.Width; j++) // { // GreyImage[j, i] = (int)((imagePointer1[0] + imagePointer1[1] + imagePointer1[2]) / 3.0); // //4 bytes per pixel // imagePointer1 += 4; // }//end for j // //4 bytes per pixel // imagePointer1 += bitmapData1.Stride - (bitmapData1.Width * 4); // }//end for i //}//end unsafe //image.UnlockBits(bitmapData1); return; }
// NOTE: This was slow, but is now much faster now that LockBits is used! public static Bitmap SetBlackToWhite(Bitmap _currentBitmap) { Bitmap temp = (Bitmap)_currentBitmap; Bitmap bmap = (Bitmap)temp.Clone(); LockBitmap lockBitmap = new LockBitmap(bmap); lockBitmap.LockBits(); Color c; for (int i = 0; i < bmap.Width; i++) { for (int j = 0; j < bmap.Height; j++) { c = bmap.GetPixel(i, j); if (c.R == 0 && c.G == 0 && c.B == 0) bmap.SetPixel(i, j, Color.FromArgb(255, 255, 255)); } } // _currentBitmap = (Bitmap)bmap.Clone(); lockBitmap.UnlockBits(); return bmap; }
public Bitmap DisplayImage(int[,] GreyImage) { //int i, j; int W, H; W = GreyImage.GetLength(0); H = GreyImage.GetLength(1); Bitmap image = new Bitmap(W, H); LockBitmap lockBitmap = new LockBitmap(image); lockBitmap.LockBits(); for (int y = 0; y < lockBitmap.Height; y++) { for (int x = 0; x < lockBitmap.Width; x++) { lockBitmap.SetPixel(x, y, Color.FromArgb((byte)GreyImage[x, y], (byte)GreyImage[x, y], (byte)GreyImage[x, y])); } } lockBitmap.UnlockBits(); //BitmapData bitmapData1 = image.LockBits(new Rectangle(0, 0, W, H), // ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); //unsafe //{ // byte* imagePointer1 = (byte*)bitmapData1.Scan0; // for (i = 0; i < bitmapData1.Height; i++) // { // for (j = 0; j < bitmapData1.Width; j++) // { // // write the logic implementation here // imagePointer1[0] = (byte)GreyImage[j, i]; // imagePointer1[1] = (byte)GreyImage[j, i]; // imagePointer1[2] = (byte)GreyImage[j, i]; // imagePointer1[3] = (byte)255; // //4 bytes per pixel // imagePointer1 += 4; // } //end for j // //4 bytes per pixel // imagePointer1 += (bitmapData1.Stride - (bitmapData1.Width * 4)); // }//End for i //}//end unsafe //image.UnlockBits(bitmapData1); return image;// col; }