Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 86,374 VB.NET Programmers. There are 1,415 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a VB.NET Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

[C#] Fast acces to bitmap pixels?

 
Reply to this topicStart new topic

[C#] Fast acces to bitmap pixels?

microchip
post 19 Feb, 2006 - 06:20 AM
Post #1


New D.I.C Head

*
Joined: 14 Aug, 2005
Posts: 37



Hi everyone
I've been trying to mess around with image recognition in C# for some time now. But to do that, I need acces to all the pixels in the bitmap individually. At the moment, I'm using getPixel and setPixel. It works, but it's VERY slow. Is there any faster way to acces pixels in a bitmap? Unsafe code maybe?
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


Amadeus
post 19 Feb, 2006 - 08:43 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 11,385

I'm afriad I don't know very much about working with images programatically...are you just using the Bitmap class? I seem to remember that working in unsafe mode could speed things up, but I've never performed the comparison.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

microchip
post 19 Feb, 2006 - 09:41 AM
Post #3


New D.I.C Head

*
Joined: 14 Aug, 2005
Posts: 37

Jup, the bitmap class. The problem is, to access the pixels unsafly, I need the starting adress of the array (assuming it is an array internally), and the format.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

microchip
post 8 Mar, 2006 - 10:59 AM
Post #4


New D.I.C Head

*
Joined: 14 Aug, 2005
Posts: 37

Okay, I'm back with a solution. With some help of a msdn article, I made this class smile.gif :

CODE

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace SampleGrabberNET
{
   public unsafe class UnsafeBitmap
   {
       Bitmap bitmap;

 // three elements used for MakeGreyUnsafe
 int width;
 BitmapData bitmapData = null;
 Byte* pBase = null;

 public UnsafeBitmap(Bitmap bitmap)
 {
     this.bitmap = new Bitmap(bitmap);
 }

       public UnsafeBitmap(int width, int height)
       {
           this.bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
       }

 public void Dispose()
 {
     bitmap.Dispose();
 }

 public Bitmap Bitmap
 {
     get
     {
   return(bitmap);
     }
 }

 private Point PixelSize
 {
     get
     {
   GraphicsUnit unit = GraphicsUnit.Pixel;
   RectangleF bounds = bitmap.GetBounds(ref unit);

   return new Point((int) bounds.Width, (int) bounds.Height);
     }
 }

 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);

     pBase = (Byte*) bitmapData.Scan0.ToPointer();
 }

 public PixelData GetPixel(int x, int y)
 {
           PixelData returnValue = *PixelAt(x, y);
     return returnValue;
 }

       public void SetPixel(int x, int y, PixelData colour)
       {
           PixelData* pixel = PixelAt(x, y);
           *pixel = colour;
       }

 public void UnlockBitmap()
 {
     bitmap.UnlockBits(bitmapData);
     bitmapData = null;
     pBase = null;
 }
       public PixelData* PixelAt(int x, int y)
       {
           return (PixelData*)(pBase + y * width + x * sizeof(PixelData));
       }
   }
   public struct PixelData
   {
       public byte blue;
       public byte green;
       public byte red;
   }
}


Use it, if you find it useful smile.gif

You can use it like this:

UnsafeBitmap your_fast_bitmap = new UnsafeBitmap(old_safe_bitmap);
your_fast_bitmap.LockBitmap();
PixelData pixel = your_fast_bitmap.getPixel(3, 4);
//more setpixel/getpixel calls here!
your_fast_bitmap.UnlockBitmap();

Here's the article on msdn that deals with unsafe bitmap access: http://msdn.microsoft.com/library/default....arp11152001.asp

This post has been edited by microchip: 8 Mar, 2006 - 11:04 AM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

woodjom
post 8 May, 2008 - 08:04 AM
Post #5


New D.I.C Head

*
Joined: 8 May, 2008
Posts: 5

I have a question....what if i am wanting to look for a pattern within a screen shot. Specifically for mapping of game areas? I have a VB.net form that will automate screenshots ever few seconds but currently am unable to find a way to do a matching algorithm based on a predefined pattern. Reason for the pattern is cause different users may have this pattern located in different areas of the screen during play.

this would be the pattern area:
IPB Image

The gold area ring for the most part....not too worried about the compass points. If i could streamline this before the screenshot that would be great but at minimal....taking the screenshot and then searching the bitmap created for this pattern is the least i want to be able to do.

I realize this is the C# board but i can easily transverse from c# to vb.net with little to no problem. This thread just seems to be the more in line with what i am trying to do but is not the exact solution i am looking for. At least point me in the correct direction smile.gif

This post has been edited by woodjom: 8 May, 2008 - 08:12 AM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/17/08 02:50AM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month