473,804 Members | 3,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bitmap.LockBits fails to work as advertised..... !?


I dont know if I missed something in documentation,
but I was having a problem with "Bitmap.LockBit s" and
was wondering if anyone else has discovered it too.

According to everyones examples including some of
the MVP's website examples, one could use unmanaged
code to manipulate a bitmap by casting the IntPtr from
the Bitmap.LockBits to say an (int*).

The problem I found is that the bitmap would rarely
move around in memory throwing a fault if you were
in the middle of manipulating a bmp.

My application was a cute little scrolling program
doing a 600x800 bmp at about 30fps. It usualy took about
5 to 10minutes before it tanked. (surely directx and
manipulating textures would have been faster, but thats
not the point.)

I found that if I specified the bitmaps data via another
one of the bitmap constructors, then pinning the memory
in the GC then and only then would the program be 100%
stable.

The only thing I know the Bitmap.LockBits does is lock
out a portion of the bitmap to prevent cross threading issues.

Has anyone else had this problem? Any other solutions while
still using pointers to manipulate data?


Mar 1 '07 #1
2 3855
TheMadHatter wrote:
According to everyones examples including some of
the MVP's website examples, one could use unmanaged
code to manipulate a bitmap by casting the IntPtr from
the Bitmap.LockBits to say an (int*).

The problem I found is that the bitmap would rarely
move around in memory throwing a fault if you were
in the middle of manipulating a bmp.
Yes, the bitmap may move around, but that is not a problem as you are
working with a pinned copy of a part of the bitmap data. Or, at least
you should be. If you are trying to manipulate the bitmap data directly,
you would get that problem.
My application was a cute little scrolling program
doing a 600x800 bmp at about 30fps. It usualy took about
5 to 10minutes before it tanked. (surely directx and
manipulating textures would have been faster, but thats
not the point.)

I found that if I specified the bitmaps data via another
one of the bitmap constructors, then pinning the memory
in the GC then and only then would the program be 100%
stable.

The only thing I know the Bitmap.LockBits does is lock
out a portion of the bitmap to prevent cross threading issues.
No, that's not what it does. It copies the data into a memory buffer and
pins the buffer in memory so that the garbage collector won't move it.
The data that you are working with is protected from other threads, but
that is just because the data is local to your thread.

--
Göran Andersson
_____
http://www.guffa.com
Mar 1 '07 #2
No, that's not what it does. It copies the data into a memory buffer and
pins the buffer in memory so that the garbage collector won't move it.
The data that you are working with is protected from other threads, but
that is just because the data is local to your thread.
That's what I thought it was suppose to do, but on a single thread program,
it still managed to move. I used LockBits at the beginning sub on the entire
area of the bmp, then used unlock at the end. I fail to see the flaw in that.

Thanks for the responce.
Mar 1 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
68562
by: Sergio Florez M. | last post by:
How can I convert a Bitmap to a byte array? I've searched all over and none of the code samples I've managed to find work. Actually, I just need to create a Byte array starting from an Image object. -- El que persevera insiste Sergio Florez M. Medellín, Colombia
2
3708
by: James Dean | last post by:
I create a bitmap like this. The width and height are got from the compressed file i am reading. The width and height are in pixels.....1bpp bitmap = new Bitmap(Width,Height,PixelFormat.Format24bppRgb); i have a bitmapdata class and then lock the bits like this. bitmapData = bitmap.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
3
9392
by: halise | last post by:
Hi, i am in trouble with an exception in .net platform with c#. The exception thrown is exactly as follows: System.ArgumentException: Invalid parameter used. at System.Drawing.Bitmap.SetPixel(Int32 x, Int32 y, Color color) at binaryData2Image.Form1.button_Click(Object sender, EventArgs e) in c:\documents and settings\..\my documents\visual studio
4
23889
by: Ma Xiaoguang | last post by:
Dear ladies and gentlemen: How to access all the pixels of a bitmap with C#? I am working with Delphi before. In Delphi, you can use TBitmap.Scanline property to access all the pixels of a bitmap object. How to do this with C#? Any hints? Thank you very much. Best regards. Xiaoguang
5
5863
by: mallolnolla | last post by:
Hello there, I'm trying to display a grabbed picture from a matrox meteor II to a picturebox (I know I can use the matrox display, but I need to show it on a PictureBox). I've tried to fill the bitmap with the setpixel method, but it's too much slow (my camera's resolution is about 700x500 pixels). Does anybody know how to fill the bitmap from a 3-dimensional array (width x height x R/G/B), or even 3 2-dimensional arrays?
5
7380
by: Lance | last post by:
I need to create a Drawing.Bitmap from an array of integer values. My current technique creates a bitmap that eventually becomes corrupt (i.e., the bitmap's pixels change to a different color after a while). Can somebody please tell me what I'm doing wrong? Here is a sample: Public Shared Function CreateBitmapFromArray( _ ByVal width As Integer, _ ByVal height As Integer, _ ByVal pixelFormat As Drawing.Imaging.PixelFormat, _
2
7779
by: cosminb | last post by:
I have a native function which returns an image in the form of a BYTE* lpDIB array. How do I convert it to a Bitmap, so that I can work with it properly in C#? Thanks in advance, Cosmin
3
3121
by: Iwanow | last post by:
Hello! My goal is to develop a program that opens a bitmap, copies its pixels to an ArrayList in order to perform some complex calculations (edge detection, Hough transform etc.), and save resulting image back in some other bitmap. It works pretty fast, except for stages of coping pixels between bitmap (object of type Bitmap) and the ArrayList. I'm using the trivial
7
8221
by: RB0135 | last post by:
Hi All, I have some Windows BMP, 1BPP, monochrome files that I need to get the raw data from to load a graphics buffer on a Roll Printer (which I know can be done). Lets forget about the Roll Printer in this equation as I am not sure I am getting the correct "raw" image from the file. However, when I print what I think is the raw data, I get the image size, but the information within the image looks nothing like it should be, and...
0
10567
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5515
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2983
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.