Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Issue 1175 fix toBitmap() to ignore padding bytes

git-svn-id: http://zxing.googlecode.com/svn/trunk@2217 59b500cc-1b3d-0410-9834-0bbf25fbcc57
  • Loading branch information...
commit ec5ff15f9d3496bc1301013ca6f4f502edd2bfd4 1 parent bb443ab
srowen authored
Showing with 35 additions and 35 deletions.
  1. +35 −35 csharp/common/ByteMatrix.cs
View
70 csharp/common/ByteMatrix.cs
@@ -131,44 +131,44 @@ public override System.String ToString()
return result.ToString();
}
- /// <summary>
- /// Converts this ByteMatrix to a black and white bitmap.
- /// </summary>
- /// <returns>A black and white bitmap converted from this ByteMatrix.</returns>
- public Bitmap ToBitmap()
- {
- const byte BLACK = 0;
- const byte WHITE = 255;
- sbyte[][] array = this.Array;
- int width = this.Width;
- int height = this.Height;
- byte[] pixels = new byte[width * height];
-
- for (int y = 0; y < height; y++)
- {
- int offset = y * width;
- for (int x = 0; x < width; x++)
- {
- pixels[offset + x] = array[y][x] == 0 ? BLACK : WHITE;
- }
- }
-
- //Here create the Bitmap to the known height, width and format
- Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
+ /// <summary>
+ /// Converts this ByteMatrix to a black and white bitmap.
+ /// </summary>
+ /// <returns>A black and white bitmap converted from this ByteMatrix.</returns>
+ public Bitmap ToBitmap()
+ {
+ const byte BLACK = 0;
+ const byte WHITE = 255;
+ int width = Width;
+ int height = Height;
- //Create a BitmapData and Lock all pixels to be written
- BitmapData bmpData =
- bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
- ImageLockMode.WriteOnly, bmp.PixelFormat);
+ // create the bitmap and lock the bits because we need the stride
+ // which is the width of the image and possible padding bytes
+ var bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
+ var bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
+ try
+ {
+ var pixels = new byte[bmpData.Stride*height];
- //Copy the data from the byte array into BitmapData.Scan0
- Marshal.Copy(pixels, 0, bmpData.Scan0, pixels.Length);
+ for (int y = 0; y < height; y++)
+ {
+ var offset = y*bmpData.Stride;
+ for (var x = 0; x < width; x++)
+ {
+ pixels[offset + x] = this[x, y] ? BLACK : WHITE;
+ }
+ }
- //Unlock the pixels
- bmp.UnlockBits(bmpData);
+ //Copy the data from the byte array into BitmapData.Scan0
+ Marshal.Copy(pixels, 0, bmpData.Scan0, pixels.Length);
+ }
+ finally
+ {
+ //Unlock the pixels
+ bmp.UnlockBits(bmpData);
+ }
- //Return the bitmap
- return bmp;
- }
+ return bmp;
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.