What does the line imagePointer1 += bitmapData1.Stride - (bitmapData1.Width * 4); mean in the following code?
var width = bitmap.Width;
var height = bitmap.Width;
var horizontalProjection = new double[width];
var verticalProjection = new double[height];
var bitmapData1 = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
unsafe
{
var imagePointer1 = (byte*)bitmapData1.Scan0;
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
var blu = imagePointer1[0];
var green = imagePointer1[1];
var red = imagePointer1[2];
int luminosity = (byte)(((0.2126 * red) + (0.7152 * green)) + (0.0722 * blu));
horizontalProjection[x] += luminosity;
verticalProjection[y] += luminosity;
imagePointer1 += 4;
}
imagePointer1 += bitmapData1.Stride - (bitmapData1.Width * 4);
}
}
MaximizeScale(ref horizontalProjection, height);
MaximizeScale(ref verticalProjection, width);
var projections =
new[]
{
horizontalProjection,
verticalProjection
};
bitmap.UnlockBits(bitmapData1);
return projections;
The code is simply incrementing the pointer contained in the variable imagePointer1.
The reason you have to increment by the stride - (width * 4) is because of the way bitmaps are represented in memory.
There's a very comprehensive explanation available as part of Bob Powell's GDI+ FAQ.
Related
Below is my code:
private double GetImageValue(Bitmap Image)
{
double ImageValue = 0;
for (int X = 0; X < Image.Width; X++)
{
for (int Y = 0; Y < Image.Height; Y++)
{
Color CurrentPixel = Image.GetPixel(X, Y);
ImageValue += CurrentPixel.A + CurrentPixel.B + CurrentPixel.G + CurrentPixel.R;
}
}
return ImageValue;
}
The code returns the total value of each pixel in the image. Is there a way to speed up the procedure?
Something like this:
private double GetImageValue(Bitmap image)
{
double imageValue = 0;
var rect = new Rectangle(0, 0, image.Width, image.Height);
var bmpData = image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
var ptr = bmpData.Scan0;
var bytes = Math.Abs(bmpData.Stride) * image.Height;
var values = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, values, 0, bytes);
for (int y = 0; y < image.Height; y++)
{
int lineStart = y * Math.Abs(bmpData.Stride);
for (int x = 0; x < image.Width * 3; x++)
{
imageValue += values[lineStart + x];
}
}
image.UnlockBits(bmpData);
return imageValue;
}
The main goal is to get all pixels from the bitmap very fast, and now this is done in 30fps.
I save the bitmap to byte[] array which has length: 4196406(4mb).
This is the code I use which give me ~5fps:
using (var data = new MemoryStream())
{
bitmap.Save(data, ImageFormat.Bmp);
ScreenRefreshed?.Invoke(this, data.ToArray());
_init = true;
}
Color[,] Pixels = new Color[Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height];
Pixels = GetPixels(data);
public Color[,] GetPixels(byte[] data)
{
int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;
Int32 stride = width * 4;
Int32 curRowOffs = (((width * height * 4) + 54) - 1) - stride;
Color[,] buffer = new Color[width, height];
for (uint y = 0; y < height; y++)
{
uint index = (uint)curRowOffs;
for (uint x = 0; x < width; x++)
{
if (index >= 0)
{
var b = data[index];
var g = data[index + 1];
var r = data[index + 2];
var a = data[index + 3];
buffer[x, y] = Color.FromArgb(a,r,g,b); //This line is reducing the fps
}
index += 4;
}
curRowOffs -= stride;
}
return buffer;
}
How to assign the values of array to another one
and loop through the second array keeping the same speed(30fps)?
This line: buffer[x, y] = Color.FromArgb(a,r,g,b); - reduces fps from 30 to 5
I have an Bitmap with various color patterns and I need to find the bounding rectangles of one given color (For example: Red) within the Bitmap. I found some code to process images but unable to figure out how to achieve this.
Any help would be highly appreciated.
This is my code.
private void LockUnlockBitsExample(PaintEventArgs e)
{
// Create a new bitmap.
Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
byte[] rgbValues = new byte[bytes];
// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
// Set every third value to 255. A 24bpp bitmap will look red.
for (int counter = 2; counter < rgbValues.Length; counter += 3)
rgbValues[counter] = 255;
// Copy the RGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
// Unlock the bits.
bmp.UnlockBits(bmpData);
// Draw the modified image.
e.Graphics.DrawImage(bmp, 0, 150);
}
Edit: The Bitmap contains solid color shapes, multiple shapes with same color can appear. I need to find the bounding rectangle of each shape.
Just like the paint fills color with bucket tool, I need the bounding rectangle of the filled area.
I can provide x, y coordinates of point on Bitmap to find the bound rectangle of color.
You would do this just like any other code where you want to find the min or max value in a list. With the difference that you want to find both min and max in both X and Y dimensions. Ex:
public static Rectangle GetBounds(this Bitmap bmp, Color color)
{
int minX = int.MaxValue;
int minY = int.MaxValue;
int maxX = int.MinValue;
int maxY = int.MinValue;
for (int y = 0; y < bmp.Height; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
var c = bmp.GetPixel(x, y);
if (color == c)
{
if (x < minX) minX = x;
if (x > maxX) maxX = x;
if (y < minY) minY = y;
if (y > maxY) maxY = y;
}
}
}
var width = maxX - minX;
var height = maxY - minY;
if (width <= 0 || height <= 0)
{
// Handle case where no color was found, or if color is a single row/column
return default;
}
return new Rectangle(minX, minY, width, height);
}
There are plenty of resources on how to use LockBits/pointers. So converting the code to use this instead of GetPixel is left as an exercise.
If you are not concerned with the performance, and an exact color match is enough for you, then just scan the bitmap:
var l = bmp.Width; var t = bmp.Height; var r = 0; var b = 0;
for (var i = 0; i<rgbValues.Length, i++)
{
if(rgbValues[i] == 255) // rgb representation of red;
{
l = Math.Min(l, i % bmpData.Stride); r = Math.Max(r, i % bmpData.Stride);
t = Math.Min(l, i / bmpData.Stride); b = Math.Max(b, i / bmpData.Stride);
}
}
if(l>=r) // at least one point is found
return new Rectangle(l, t, r-l+1, b-t+1);
else
return new Rectangle(0, 0, 0, 0); // nothing found
You can search for the first point of each shape that fills a different area on the Bitmap, read a single horizontal row to get the points of the given color, then loop vertically within the horizontal range to get the adjacent points.
Once you get all the points of each, you can calculate the bounding rectangle through the first and last points.
public static IEnumerable<Rectangle> GetColorRectangles(Bitmap src, Color color)
{
var rects = new List<Rectangle>();
var points = new List<Point>();
var srcRec = new Rectangle(0, 0, src.Width, src.Height);
var srcData = src.LockBits(srcRec, ImageLockMode.ReadOnly, src.PixelFormat);
var srcBuff = new byte[srcData.Stride * srcData.Height];
var pixSize = Image.GetPixelFormatSize(src.PixelFormat) / 8;
Marshal.Copy(srcData.Scan0, srcBuff, 0, srcBuff.Length);
src.UnlockBits(srcData);
Rectangle GetColorRectangle()
{
var curX = points.First().X;
var curY = points.First().Y + 1;
var maxX = points.Max(p => p.X);
for(var y = curY; y < src.Height; y++)
for(var x = curX; x <= maxX; x++)
{
var pos = (y * srcData.Stride) + (x * pixSize);
var blue = srcBuff[pos];
var green = srcBuff[pos + 1];
var red = srcBuff[pos + 2];
if (Color.FromArgb(red, green, blue).ToArgb().Equals(color.ToArgb()))
points.Add(new Point(x, y));
else
break;
}
var p1 = points.First();
var p2 = points.Last();
return new Rectangle(p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y);
}
for (var y = 0; y < src.Height; y++)
{
for (var x = 0; x < src.Width; x++)
{
var pos = (y * srcData.Stride) + (x * pixSize);
var blue = srcBuff[pos];
var green = srcBuff[pos + 1];
var red = srcBuff[pos + 2];
if (Color.FromArgb(red, green, blue).ToArgb().Equals(color.ToArgb()))
{
var p = new Point(x, y);
if (!rects.Any(r => new Rectangle(r.X - 2, r.Y - 2,
r.Width + 4, r.Height + 4).Contains(p)))
points.Add(p);
}
}
if (points.Any())
{
var rect = GetColorRectangle();
rects.Add(rect);
points.Clear();
}
}
return rects;
}
Demo
private IEnumerable<Rectangle> shapesRects = Enumerable.Empty<Rectangle>();
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
var sx = 1f * pictureBox1.Width / pictureBox1.ClientSize.Width;
var sy = 1f * pictureBox1.Height / pictureBox1.ClientSize.Height;
var p = Point.Round(new PointF(e.X * sx, e.Y * sy));
var c = (pictureBox1.Image as Bitmap).GetPixel(p.X, p.Y);
shapesRects = GetColorRectangles(pictureBox1.Image as Bitmap, c);
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (shapesRects.Any())
using (var pen = new Pen(Color.Black, 2))
e.Graphics.DrawRectangles(pen, shapesRects.ToArray());
}
When I run this code
Bitmap im = new Bitmap(600, 600, PixelFormat.Format16bppGrayScale);
int height = im.Height;
int width = im.Width;
Point p1 = new Point(0, 0);
Point p2 = new Point(im.Size.Width, 0);
Point p3 = new Point(im.Width / 2, im.Height);
Random r = new Random();
Point p = new Point(r.Next(0, im.Size.Width), r.Next(0, im.Size.Height));
BitmapData data = im.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format16bppGrayScale);
const int stride = 2;
int wsrtide = data.Stride;
unsafe
{
IntPtr osc0 = data.Scan0;
ushort* sc0 = (ushort*)data.Scan0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
*sc0 = 0;
sc0 += 1;
}
}
for (long i = 0; i < width * height; i++)
{
if (i % 1_000_000 == 0)
{
Console.WriteLine(i);
}
var ran = r.Next(0, 3);
Point tp;
switch (ran)
{
case 0:
tp = new Point((p1.X + p.X) / 2, (p1.Y + p.Y) / 2);
sc0 = (ushort*)((int)osc0 + (wsrtide * tp.Y + (tp.X * stride)));
*sc0 = ushort.MaxValue;
p = tp;
break;
case 1:
tp = new Point((p2.X + p.X) / 2, (p2.Y + p.Y) / 2);
sc0 = (ushort*)((int)osc0 + (wsrtide * tp.Y + (tp.X * stride)));
*sc0 = ushort.MaxValue;
p = tp;
break;
case 2:
tp = new Point((p3.X + p.X) / 2, (p3.Y + p.Y) / 2);
sc0 = (ushort*)((int)osc0 + (wsrtide * tp.Y + (tp.X * stride)));
*sc0 = ushort.MaxValue;
p = tp;
break;
}
}
im.UnlockBits(data);
im.Save(Environment.CurrentDirectory + "\\img.png");
im.Dispose();
It throws where I save the bitmap
System.Runtime.InteropServices.ExternalException: A generic error
occurred in GDI+.`
I am almost certain that this isn't caused by file permissions, when I give the program admin permission and delete the image to reset permissions it still throws. I can confirm that this code swapped with BitmapSetPixel() on RGB works.
My suspicion is that I'm messing up the pointers, but I'm not really sure. Also, curiously enough, it makes empty png files even though it throws.
The purpose of this code is to generate a Sierpinski triangle using the chaos game method.
Your problem is the Format16bppGrayScale i don't think the GDI supports it very well.
Basically if you just create the Bitmap in Format16bppGrayScale, and save it with nothing else, it still gives the error.
I have taken the liberty to rewrite your method Format32bppPArgb
private unsafe static void Main(string[] args)
{
var height = 600;
var width = 600;
var p1 = new Point(0, 0);
var p2 = new Point(width, 0);
var p3 = new Point(width / 2, height);
var r = new Random();
var p = new Point(r.Next(0, width), r.Next(0, width));
using (var im = new Bitmap(width, height, PixelFormat.Format32bppPArgb))
{
var data = im.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);
var sc0 = (int*)data.Scan0;
var pLen = sc0 + height * width;
var black = Color.Black.ToArgb();
var white = Color.White.ToArgb();
for (var pI = sc0; pI < pLen; pI++)
*pI = black;
for (long i = 0; i < width * height; i++)
{
Point tp;
switch (r.Next(0, 3))
{
case 0:
tp = new Point((p1.X + p.X) / 2, (p1.Y + p.Y) / 2);
*(sc0 + tp.Y + tp.X * width) = white;
p = tp;
break;
case 1:
tp = new Point((p2.X + p.X) / 2, (p2.Y + p.Y) / 2);
*(sc0 + tp.Y + tp.X * width) = white;
p = tp;
break;
case 2:
tp = new Point((p3.X + p.X) / 2, (p3.Y + p.Y) / 2);
*(sc0 + tp.Y + tp.X * width) = white;
p = tp;
break;
}
}
im.UnlockBits(data);
im.Save(#"D:\img.png", ImageFormat.Png);
}
}
Result
You can convert it after the fact if you want, add pepper and salt to taste
Also if you get rid of all the points and cache the colors this will be a bit faster
I have some code which has the error "AccessViolationException was unhandled by user code: Attempted to read or write protected memory..."
A trimmed down version of the offending function is as follows:
protected override void OnPaint(PaintEventArgs pe)
{
if ((updatingFastBackground) || (Calculating)) return; //ADDED FOR DEBUGGING, SEE BELOW
BitmapData canvasData = Canvas.LockBits(new Rectangle(Point.Empty, Canvas.Size), ImageLockMode.WriteOnly, FastPixelFormat);
BitmapData fbgData = fastBackground.LockBits(new Rectangle(Point.Empty, fastBackground.Size), ImageLockMode.ReadOnly, FastPixelFormat);
try
{
unsafe
{
byte* canvasDataScan0 = (byte*)canvasData.Scan0.ToPointer();
byte* fbgDataScan0 = (byte*)fbgData.Scan0.ToPointer();
Rectangle spriteBounds = new Rectangle(Point.Empty, ButtonImages.ImageSize);
for (int i = 0; i < ButtonImages.Images.Count; i++)
{
// Button offset location
Point l = new Point(
(int)((i % columnCount) * hStep + myIVM.Location.X),
(int)((i / columnCount) * vStep + myIVM.Location.Y));
// Paint at current location?
if (buttonPaintBounds.Contains(l))
{
BitmapData spriteData = buttonBitmaps[i].LockBits(spriteBounds, ImageLockMode.ReadOnly, FastPixelFormat);
try
{
int spriteLeft = Math.Max(l.X, 0);
int spriteRight = Math.Min(l.X + ButtonImages.ImageSize.Width, canvasData.Width);
int spriteTop = Math.Max(l.Y, 0);
int spriteBottom = Math.Min(l.Y + ButtonImages.ImageSize.Height, canvasData.Height);
int spriteWidth = spriteRight - spriteLeft;
int spriteHeight = spriteBottom - spriteTop;
byte* canvasRowLeft = canvasDataScan0 + (spriteTop * canvasData.Stride) + spriteLeft * 4;
byte* spriteRowLeft =
(byte*)spriteData.Scan0.ToPointer() +
Math.Max((spriteTop - l.Y), 0) * spriteData.Stride +
Math.Max((spriteLeft - l.X), 0) * 4;
for (int y = 0; y < spriteHeight; y++)
{
canvasRowLeft += canvasData.Stride;
spriteRowLeft += spriteData.Stride;
Byte* canvasWalk = (Byte*)canvasRowLeft;
Byte* spriteWalk = (Byte*)spriteRowLeft;
for (int x = 0; x < spriteWidth; x++)
{
if (spriteWalk[3] != 255)
{
canvasWalk[0] = (byte)(canvasWalk[0] * spriteWalk[3] / 255 + spriteWalk[0]);
canvasWalk[1] = (byte)(canvasWalk[1] * spriteWalk[3] / 255 + spriteWalk[1]);
canvasWalk[2] = (byte)(canvasWalk[2] * spriteWalk[3] / 255 + spriteWalk[2]);
}
canvasWalk += 4;
spriteWalk += 4;
}
}
thesePoints.Add(l);
}
finally
{
buttonBitmaps[i].UnlockBits(spriteData);
}
}
The error occurs on the line:
canvasWalk[0] = (byte)(canvasWalk[0] * spriteWalk[3] / 255 + spriteWalk[0]);
and even when replaced with:
canvasWalk[0] = 0;
The iteration variables y and x have different values each time it crashes so this leads me to believe an external function is modifying the Canvas bitmap.
IF this is in fact my problem, is there a way to prevent fastBackground and Canvas from being externally modified? I thought LockBits was supposed to do that that...
If that's not enough to answer, here's some more I've tried:
I added the line
if ((updatingFastBackground) || (Calculating)) return;
to exit OnPaint if fastBackground Canvas or dimensions are being modified by other functions.
I could use a mutex to prevent the functions that modify the bitmaps fastBackground and Canvas from being run at the same time as paint (as I think they must be) but I'd rather block them another way as Canvas is public and I don't want to require passing a mutex out of the class.
per #usr 's suggestion, this further trimmed down version doesn't fail... Must have been a PTD error. (programmer too dumb) i.e. arithmetic error
protected override void OnPaint(PaintEventArgs pe)
{
if ((updatingFastBackground) || (Calculating)) return; //ADDED FOR DEBUGGING, SEE BELOW
BitmapData canvasData = Canvas.LockBits(new Rectangle(Point.Empty, Canvas.Size), ImageLockMode.WriteOnly, FastPixelFormat);
BitmapData fbgData = fastBackground.LockBits(new Rectangle(Point.Empty, fastBackground.Size), ImageLockMode.ReadOnly, FastPixelFormat);
try
{
unsafe
{
byte* canvasDataScan0 = (byte*)canvasData.Scan0.ToPointer();
byte* fbgDataScan0 = (byte*)fbgData.Scan0.ToPointer();
Rectangle spriteBounds = new Rectangle(Point.Empty, ButtonImages.ImageSize);
for (int i = 0; i < ButtonImages.Images.Count; i++)
{
// Button offset location
Point l = new Point(
(int)((i % columnCount) * hStep + myIVM.Location.X),
(int)((i / columnCount) * vStep + myIVM.Location.Y));
// Paint at current location?
if (buttonPaintBounds.Contains(l))
{
BitmapData spriteData = buttonBitmaps[i].LockBits(spriteBounds, ImageLockMode.ReadOnly, FastPixelFormat);
try
{
byte* canvasRowLeft = canvasDataScan0;
byte* spriteRowLeft = (byte*)spriteData.Scan0.ToPointer();
for (int y = 0; y < 145; y++)
{
canvasRowLeft += canvasData.Stride;
spriteRowLeft += spriteData.Stride;
Byte* canvasWalk = (Byte*)canvasRowLeft;
Byte* spriteWalk = (Byte*)spriteRowLeft;
for (int x = 0; x < 145; x++)
{
if (spriteWalk[3] != 255)
{
canvasWalk[0] = 0;
canvasWalk[1] = 0;
canvasWalk[2] = 0;
}
canvasWalk += 4;
spriteWalk += 4;
}
}
thesePoints.Add(l);
}
finally
{
buttonBitmaps[i].UnlockBits(spriteData);
}
}
Moving my comment into an answer because it helped solve the problem:
Fixed is not required for the buffer returned by LockBits because it is unmanaged memory. Your pointer arithmetic is wrong. Find the bug. Create a simple repro to help find the bug.