473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Blitting

Is it faster to modify array of ints as 32-bit pixel data and then somehow
turn that into a bitmap or use lockbits? I'm having a little performance
issues with lock bits unfortunately. I figure that I shouldn't have any
issues writing to an array and then maybe there is a faster way to convert
that into a bitmap than using lockbits?
Thanks,
Jon
Sep 10 '07 #1
6 5415
Hi John,
What performance issues are you having with LockBits? If you're programming
C# then you should surely be using unsafe code and not the Marshal class for
access.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:gC******************@newssvr11.news.prodigy.n et...
Is it faster to modify array of ints as 32-bit pixel data and then somehow
turn that into a bitmap or use lockbits? I'm having a little performance
issues with lock bits unfortunately. I figure that I shouldn't have any
issues writing to an array and then maybe there is a faster way to convert
that into a bitmap than using lockbits?
Thanks,
Jon
Sep 10 '07 #2

"Bob Powell [MVP]" <bo*@spamkillerbobpowell.netwrote in message
news:CE**********************************@microsof t.com...
Hi John,
What performance issues are you having with LockBits? If you're
programming C# then you should surely be using unsafe code and not the
Marshal class for access.

Did you look at my code? I'm not sure what you mean by unsafe and
marshaling? I used unsafe to access the lockbits... its all in the code I
pasted...

crap... lol, I guess I didn't paste any code ;/

Ok, here it is... Its kinda messy but shows what I'm doing when using
lockbits(I pretty much copied and pasted from some other site that said to
use this method.

When I run this under VS I get about 1fps. When I run it outside of VS I get
about 10-20fps or so. (it might actually be fast but its slightly jerky)
Thanks,
Jon

bmd = bitmap.LockBits(R, System.Drawing.Imaging.ImageLockMode.ReadOnly,
bitmap.PixelFormat);

unsafe

{

for (int y = 0; y < bmd.Height; y++)

{

byte* row = (byte*)bmd.Scan0 + (y * bmd.Stride);

for (int x = 0; x < bmd.Width; x++)

{

c = (int)(t) % 255;

row[x * PixelSize + 2] = (byte)(255*(Math.Pow((c/255F),0.8)));

row[x * PixelSize + 1] = (byte)(255*(Math.Pow((c/255F),2)));

row[x * PixelSize + 0] = (byte)(255*(Math.Pow((c/255F),3)));

}

}

} // unsafe

bitmap.UnlockBits(bmd);

BitmapGraphics2.DrawImage(bitmap, 0, 0);
Sep 10 '07 #3

"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:Am****************@newssvr21.news.prodigy.net ...
>
"Bob Powell [MVP]" <bo*@spamkillerbobpowell.netwrote in message
news:CE**********************************@microsof t.com...
>Hi John,
What performance issues are you having with LockBits? If you're
programming C# then you should surely be using unsafe code and not the
Marshal class for access.


Did you look at my code? I'm not sure what you mean by unsafe and
marshaling? I used unsafe to access the lockbits... its all in the code I
pasted...

crap... lol, I guess I didn't paste any code ;/

Ok, here it is... Its kinda messy but shows what I'm doing when using
lockbits(I pretty much copied and pasted from some other site that said to
use this method.

When I run this under VS I get about 1fps. When I run it outside of VS I
get about 10-20fps or so. (it might actually be fast but its slightly
jerky)
Thanks,
Jon

bmd = bitmap.LockBits(R, System.Drawing.Imaging.ImageLockMode.ReadOnly,
bitmap.PixelFormat);
ReadOnly doesn't look right for this.
>
unsafe

{

for (int y = 0; y < bmd.Height; y++)

{

byte* row = (byte*)bmd.Scan0 + (y * bmd.Stride);

for (int x = 0; x < bmd.Width; x++)

{

c = (int)(t) % 255;

row[x * PixelSize + 2] = (byte)(255*(Math.Pow((c/255F),0.8)));

row[x * PixelSize + 1] = (byte)(255*(Math.Pow((c/255F),2)));

row[x * PixelSize + 0] = (byte)(255*(Math.Pow((c/255F),3)));

}

}

} // unsafe

bitmap.UnlockBits(bmd);

BitmapGraphics2.DrawImage(bitmap, 0, 0);

Which part is the slowdown (use Diagnostics.Stopwatch)? The LockBits,
filling in the array, UnlockBits, or DrawImage?

I doubt the computations are the bottleneck, but you can surely remove some
redundant code there, use repeated addition to find row instead of
multiplying by y, precompute x*PixelSize, and (c / 255). Also multiplying
will be much faster than Math.Pow for the integral cases.

If I'm right and the for loops are fast enough, then you can surely get a
very good frame rate (i.e. hit monitor's refresh rate with plenty of cycles
to spare) using OpenGL with glDrawPixels. A little more work for you, but
you'll get direct transfers to video memory and thence to the screen. If
you move up to a texture with glTexImage2D, a lot of effects become
essentially free at that point as well (transparency for watermarking or
fading in and out, rotation, stretching, etc).
Sep 11 '07 #4
"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:Am****************@newssvr21.news.prodigy.net ...
>
"Bob Powell [MVP]" <bo*@spamkillerbobpowell.netwrote in message
news:CE**********************************@microsof t.com...
>Hi John,

bmd = bitmap.LockBits(R, System.Drawing.Imaging.ImageLockMode.ReadOnly,
bitmap.PixelFormat);
ReadOnly works for you here? I've always gotten "attempt to write to
protected memory" exceptions whenever I tried to write to memory I've
obtained via a readonly lockbits <g>

What is the original bitmap's depth/pixel format? If I rember correctly, i
have had performance problems in the past with LockBits when using palette
(8bpp grayscale) bitmaps created from jpegs.

--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?

Sep 11 '07 #5

"Doug Semler" <do********@gmail.comwrote in message
news:em**************@TK2MSFTNGP02.phx.gbl...
"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:Am****************@newssvr21.news.prodigy.net ...
>>
"Bob Powell [MVP]" <bo*@spamkillerbobpowell.netwrote in message
news:CE**********************************@microso ft.com...
>>Hi John,

bmd = bitmap.LockBits(R, System.Drawing.Imaging.ImageLockMode.ReadOnly,
bitmap.PixelFormat);

ReadOnly works for you here? I've always gotten "attempt to write to
protected memory" exceptions whenever I tried to write to memory I've
obtained via a readonly lockbits <g>
I write ReadWrite and others and all the same result. The page I got this
code from had ReadOnly so I kepted it. Since I don't really know what it
does I kepted it like that. (even though one would expect that it would
prevent writing to the bits).
What is the original bitmap's depth/pixel format? If I rember correctly,
i have had performance problems in the past with LockBits when using
palette (8bpp grayscale) bitmaps created from jpegs.
32bit. ARGB. I guess atleast. I'm just creating a standard bitmap which I
suppose uses the display device parameters. Didn't really bother with
checking since I figure if it was something strange then it wouldn't look
right. (if it was 16 bits then and I treated it was 32bit then I wouldn't
get the correct picture).
Sep 11 '07 #6

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:On**************@TK2MSFTNGP04.phx.gbl...
>
"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:Am****************@newssvr21.news.prodigy.net ...
>>
"Bob Powell [MVP]" <bo*@spamkillerbobpowell.netwrote in message
news:CE**********************************@microso ft.com...
>>Hi John,
What performance issues are you having with LockBits? If you're
programming C# then you should surely be using unsafe code and not the
Marshal class for access.


Did you look at my code? I'm not sure what you mean by unsafe and
marshaling? I used unsafe to access the lockbits... its all in the code I
pasted...

crap... lol, I guess I didn't paste any code ;/

Ok, here it is... Its kinda messy but shows what I'm doing when using
lockbits(I pretty much copied and pasted from some other site that said
to use this method.

When I run this under VS I get about 1fps. When I run it outside of VS I
get about 10-20fps or so. (it might actually be fast but its slightly
jerky)
Thanks,
Jon

bmd = bitmap.LockBits(R, System.Drawing.Imaging.ImageLockMode.ReadOnly,
bitmap.PixelFormat);

ReadOnly doesn't look right for this.
>>
unsafe

{

for (int y = 0; y < bmd.Height; y++)

{

byte* row = (byte*)bmd.Scan0 + (y * bmd.Stride);

for (int x = 0; x < bmd.Width; x++)

{

c = (int)(t) % 255;

row[x * PixelSize + 2] = (byte)(255*(Math.Pow((c/255F),0.8)));

row[x * PixelSize + 1] = (byte)(255*(Math.Pow((c/255F),2)));

row[x * PixelSize + 0] = (byte)(255*(Math.Pow((c/255F),3)));

}

}

} // unsafe

bitmap.UnlockBits(bmd);

BitmapGraphics2.DrawImage(bitmap, 0, 0);


Which part is the slowdown (use Diagnostics.Stopwatch)? The LockBits,
filling in the array, UnlockBits, or DrawImage?

I doubt the computations are the bottleneck, but you can surely remove
some redundant code there, use repeated addition to find row instead of
multiplying by y, precompute x*PixelSize, and (c / 255). Also multiplying
will be much faster than Math.Pow for the integral cases.

If I'm right and the for loops are fast enough, then you can surely get a
very good frame rate (i.e. hit monitor's refresh rate with plenty of
cycles to spare) using OpenGL with glDrawPixels. A little more work for
you, but you'll get direct transfers to video memory and thence to the
screen. If you move up to a texture with glTexImage2D, a lot of effects
become essentially free at that point as well (transparency for
watermarking or fading in and out, rotation, stretching, etc).
Yes, I should be able to get it a good frame rate. I just need to work at
the pixel level because I am trying to do a physics simulation. The display
is only to display the result of the simulation periodically. I have some
other simulations that I've done but I didn't use lock bits and they seem
pretty fast. This is first time I'm using lock bits but potentially code is
slow when I'm actually doing the simulation(but shouldn't be that slow).

What I did notice is that when I run it under VS its about 20 times slower
even if in active mode. So the debugger is making it very slow and I
probably got confused when I was running it and with the simulation.

Essentially I'm calculating two gaussians and maybe the math library is
pretty slow. I guess I'll need to precalculate the guassians and see how
that works. I'll try to profile it sometime but distracted writing a vector
library ATM.

Thanks,
Jon
Sep 11 '07 #7

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

Similar topics

7
by: Jane Austine | last post by:
As you add more items, say text lines, in Text widget, it gets too slow and almost impractical to use on. Take idle for example. If the text gets bigger(e.g. print...
1
by: Paul E Collins | last post by:
I'm writing a fairly simple two-player puzzle game. I have a background image in a PNG file (44 KB, 160 x 320 pixels) which I load into an Image object ... imgCachedBackground =...
0
by: Nick Jacobson | last post by:
Please help! The following code is used to draw a bitmap on a form with a color key (i.e. a color that's not drawn). For speed purposes, I'm trying to call the Win32 API function:...
2
by: Nick Jacobson | last post by:
The following code is used to draw a bitmap on a form with a color key (i.e. a color that's not drawn). For speed purposes, I'm trying to call the Win32 API function: TransparentBlt. But the...
1
by: James dean | last post by:
Could someone explain how this works. I think the graphics card is used to do blitting and drawing shapes like rectangles. How does it draw using the Graphics card on the PC and why is this feature...
0
Robbie
by: Robbie | last post by:
Hi again. I am using MCI to show a video file playing in a picturebox called PicVideo. (This works fine.) SendString = "open " + Chr(34) + DialogStuff.FileName + Chr(34) + " type " + FileType +...
0
by: Duracel | last post by:
Hi, I've got a routine that fetches a bitmap from a COM server and converts it into a .NET format bitmap. The original DIB surface is 24 bit. When blitting this bitmap to a window, the speed...
11
by: dynamo | last post by:
Hi guys i have come again with more problems.This time it has to do with pygame.The following code does not give any error messages but it does not do what it is supposed to do either.the code is a...
10
by: Arnaud Diederen | last post by:
Hello everyone, and happy new year to all c.l.j'ers! No bad code to post, just a question. I wrote a JavaScript application that lets users view an image, and zoom on it. The way I "zoom"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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...

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.