Convert a bitmap with white and black areas to a list of cartesian position using C#

StackOverflow https://stackoverflow.com/questions/22675864

  •  22-06-2023
  •  | 
  •  

سؤال

Bitmap to be converted

How can I convert a bitmap to a list of custom points List<CustomPoints> myImage using C#

Class CustomPoints
{
  double X {get;set;}
  double Y {get;set;}
  bool IsBlack {get;set;}
} 
هل كانت مفيدة؟

المحلول

You can iterate through the bitmap and check each value by using the GetPixel function:

From Microsoft:

private void GetPixel_Example(PaintEventArgs e)
{
  // Create a Bitmap object from an image file.
  Bitmap myBitmap = new Bitmap("Grapes.jpg");

  // Get the color of a pixel within myBitmap.
  Color pixelColor = myBitmap.GetPixel(50, 50);

  // Fill a rectangle with pixelColor.
  SolidBrush pixelBrush = new SolidBrush(pixelColor);
  e.Graphics.FillRectangle(pixelBrush, 0, 0, 100, 100);
}

But using the GetPixel function is slow, especially when the images get larger. In that case, your are advised to use LockBits:

Use the LockBits method to lock an existing bitmap in system memory so that it can be changed programmatically. You can change the color of an image with the SetPixel method, although the LockBits method offers better performance for large-scale changes.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top