Click to See Complete Forum and Search --> : VB.NET and BitBlt? - Drawimage is tooo slooow


Tim
09-19-2001, 10:11 AM
Hi Guys,

Thanks to everyone who helped with my enquiry about transparent bit maps
on pictureboxes. I got it working but its so slow. I expect its all the wrapper
VB.NET technology thats doing it.

So seeing as I have this code written already in VB ver 6 I thought why not
use it.

Basically my orginal code uses BitBlt and Additional Mask graphics to perform
the transparency. I would like to do this again using VB.NET so as to increase
the speed back to VB 6 levels. My problem is that when I convert any program
(or start from scratch) which contains the BitBlt API call VB.NET says it
doesn't know how to interpret the command.

Has anyone discovered away of using BitBlt (and other GDI stuff) under VB.NET?

If so please advise!

Thanks in advance.

Tim

Ian R
09-19-2001, 10:22 AM
The Bitmap object has a MakeTransparent method that you might want to take a
look at. Why bother with BitBlt and raster ops when it's available in the
framework ?

"Tim " <tim.walsha@virgin.net> wrote in message
news:3ba8a78e$1@news.devx.com...
>
> Hi Guys,
>
> Thanks to everyone who helped with my enquiry about transparent bit maps
> on pictureboxes. I got it working but its so slow. I expect its all the
wrapper
> VB.NET technology thats doing it.
>
> So seeing as I have this code written already in VB ver 6 I thought why
not
> use it.
>
> Basically my orginal code uses BitBlt and Additional Mask graphics to
perform
> the transparency. I would like to do this again using VB.NET so as to
increase
> the speed back to VB 6 levels. My problem is that when I convert any
program
> (or start from scratch) which contains the BitBlt API call VB.NET says it
> doesn't know how to interpret the command.
>
> Has anyone discovered away of using BitBlt (and other GDI stuff) under
VB.NET?
>
> If so please advise!
>
> Thanks in advance.
>
> Tim

Tim
09-19-2001, 10:31 AM
Because it is so slow (like I said in my original posting)

My code in VB.NET runs significantly slower (for painting sprites to the
screen) than my version written in VB 6.

I would love to use the framework as it is so much easier once you know how.
Its just that it ruins the flow of the animation when it goes so slowly...






"Ian R" <ianr@na.net> wrote:
>The Bitmap object has a MakeTransparent method that you might want to take
a
>look at. Why bother with BitBlt and raster ops when it's available in the
>framework ?
>
>"Tim " <tim.walsha@virgin.net> wrote in message
>news:3ba8a78e$1@news.devx.com...
>>
>> Hi Guys,
>>
>> Thanks to everyone who helped with my enquiry about transparent bit maps
>> on pictureboxes. I got it working but its so slow. I expect its all the
>wrapper
>> VB.NET technology thats doing it.
>>
>> So seeing as I have this code written already in VB ver 6 I thought why
>not
>> use it.
>>
>> Basically my orginal code uses BitBlt and Additional Mask graphics to
>perform
>> the transparency. I would like to do this again using VB.NET so as to
>increase
>> the speed back to VB 6 levels. My problem is that when I convert any
>program
>> (or start from scratch) which contains the BitBlt API call VB.NET says
it
>> doesn't know how to interpret the command.
>>
>> Has anyone discovered away of using BitBlt (and other GDI stuff) under
>VB.NET?
>>
>> If so please advise!
>>
>> Thanks in advance.
>>
>> Tim
>
>

Cali LaFollett
09-19-2001, 10:49 AM
> Because it is so slow (like I said in my original posting)
>
> My code in VB.NET runs significantly slower (for painting sprites to the
> screen) than my version written in VB 6.
>
> I would love to use the framework as it is so much easier once you know
how.
> Its just that it ruins the flow of the animation when it goes so slowly...

Tim,

May want to take a look at Dan Appleman's "Moving to VB.NET" book. His
graphics sections includes an VB.NET API declare for BitBlting I believe.

Cal

Ian R
09-19-2001, 11:12 AM
"Tim" <tim.walsha@virgin.net> wrote in message
news:3ba8ac2c@news.devx.com...
>
> My code in VB.NET runs significantly slower (for painting sprites to the
> screen) than my version written in VB 6.
>
> I would love to use the framework as it is so much easier once you know
how.
> Its just that it ruins the flow of the animation when it goes so slowly...
>

Could you post some sample code ? I'm using DrawImage in one project that I
converted that used BitBlt and I don't see any noticable difference between
the .NET version and the VB6 version. I'm curious.

Larry Serflaten
09-19-2001, 04:39 PM
"Tim" <tim.walsha@virgin.net> wrote
>
> Because it is so slow (like I said in my original posting)
>
> My code in VB.NET runs significantly slower (for painting sprites to the
> screen) than my version written in VB 6.
>
> I would love to use the framework as it is so much easier once you know how.
> Its just that it ruins the flow of the animation when it goes so slowly...

Could we see your code? I am also interested in how you got VB.Net to
handle bit-masking operations....

LFS

Jay B. Harlow
09-19-2001, 07:04 PM
Tim,
Have you tried DrawImageUnscaled vs DrawImage?

From the description it sounds like it may help. I do not have a good test
to try it with. The one program I have there is no noticeable difference
(for me)...

Hope this helps
Jay

"Tim" <tim.walsha@virgin.net> wrote in message
news:3ba8ac2c@news.devx.com...
>
> Because it is so slow (like I said in my original posting)
>
> My code in VB.NET runs significantly slower (for painting sprites to the
> screen) than my version written in VB 6.
>
> I would love to use the framework as it is so much easier once you know
how.
> Its just that it ruins the flow of the animation when it goes so slowly...
>
>
>
>
>
>
> "Ian R" <ianr@na.net> wrote:
> >The Bitmap object has a MakeTransparent method that you might want to
take
> a
> >look at. Why bother with BitBlt and raster ops when it's available in the
> >framework ?
> >
> >"Tim " <tim.walsha@virgin.net> wrote in message
> >news:3ba8a78e$1@news.devx.com...
> >>
> >> Hi Guys,
> >>
> >> Thanks to everyone who helped with my enquiry about transparent bit
maps
> >> on pictureboxes. I got it working but its so slow. I expect its all the
> >wrapper
> >> VB.NET technology thats doing it.
> >>
> >> So seeing as I have this code written already in VB ver 6 I thought why
> >not
> >> use it.
> >>
> >> Basically my orginal code uses BitBlt and Additional Mask graphics to
> >perform
> >> the transparency. I would like to do this again using VB.NET so as to
> >increase
> >> the speed back to VB 6 levels. My problem is that when I convert any
> >program
> >> (or start from scratch) which contains the BitBlt API call VB.NET says
> it
> >> doesn't know how to interpret the command.
> >>
> >> Has anyone discovered away of using BitBlt (and other GDI stuff) under
> >VB.NET?
> >>
> >> If so please advise!
> >>
> >> Thanks in advance.
> >>
> >> Tim
> >
> >
>

Phil Weber
09-19-2001, 08:25 PM
Tim: You might give it another try after VB.NET ships, just to make sure the
performance degradation isn't due to debug code in the beta.
---
Phil Weber

Tim Walsha
09-20-2001, 06:29 AM
Thanks for all your responses.

Some of you asked for me to put my code up so here it is.

Let me briefly explain what I am doing.

1. I am using DrawImage (instead of Bitblt) because that is part of the runtime
and Bitblt doesn't seem to be supported anymore.

2. DrawImage seems to remove all the hardship which tranditionally was around
when doing sprite / transparent bitmap operations in VB 6. With Bitblt you
had to mask out the area where the sprite / graphic was going to be first
with a sorceAnd. Then you would 'lay over' the actual graphic with an invert.
This would provide transparent bitmaps or sprites typically found in games
(ie the edges of the character are transparent and dont rub out the background)
- takes two operations though in order to draw a sprite to the screen.

3. Addtionally to this overhead you also need to 'double-buffer' or you get
screen flicker as the sprites are moved across the screen. Screen flicker
occurs because you draw the image, pause for a while so the eye can see it,
then rub it out, then move the co-ordinates to the next place and start again.
Unfortunately if you are drawing a number of graphics the human eye picks
up the point when you have rubbed the graphics out and the sprites appear
to flicker when they move. Double buffering is used to draw all these character
graphics to an off screen surface and then with one big and fast bitblt you
move the whole off-screen surface to the main viewing surface.

4. All this seems like overkill - but it works and it provided a really cool
animation with no flicker. (Its how all games work - even 3d). An easy way
to think of this is that you have two boards. One is a construction board
and the other is a display board.

Here is the point of all this:-

I was trying for ages to find out what the equivalent VB.NET graphics commands
would be to the VB 6 stuff I mentioned above. I got a lot of help from people
on this forum and eventually got the following code to work:-

Public BoardGraphics As Graphics 'sets up graphic surface
Dim BoardBMP as Bitmap 'this is the off screen version

'set the graphics surface to point to the picture box called Board
Public BoardGraphics As Graphics
BoardGraphics = Board.CreateGraphics()

'load graphics for object and background up into objects from files
Character = Image.FromFile("..\Graphics\Telephone.bmp")
BoardBMP = Image.FromFile("..\Graphics\board.bmp")


'set the transparent colour in the bitmap to equal a colour
Dim TransparentColour As Color
TransparentColour = Color.Black ' the background to the sprites are black
Character.MakeTransparent(TransparentColour)
BoardBMP.MakeTransparent(TransparentColour)

'grab a handle to the boardBMP (where we construct the board off screen)
HandletoBoardGraphic = Graphics.FromImage(BoardBMP)

'draw character to offscreen surface
HandletoBoardGraphic.DrawImage(Character , X, Y)

'flip - this is the double buffering bit which stops flicker.
'it is the last stage of the animation process where we copy the
'built image over to the actuall screen surface
BoardGraphics.DrawImage(BoardBMP, 0, 0) ' this flips the image



Hope this makes sense. The are two problems I found with this method.
1. Drawimage seems to be an object and as such you can draw over a picturebox
and it won't destroy whats underneath. (You might think good - but the problem
stems from the fact that there is significant flicker) When you introduce
a double buffering flip - the operation transfers the background , but not
the character because it is logically separate (OO? / framework?)

2. I found that by doing all this work constructing the offscreen surface
on a bitmap instead of a picturebox (as in the code above) works and by using
the 'FromImage' command I can grab all the detail from the offscreen surface
and transfer it to the actual screen. However it is slow. (compared to the
long winded but quite quick bitblt VB 6 way of doing things)

I take the point about waiting for a release VB.NET version . It might help,
just a bit of a shame because I am sure things should go faster...

Please let me know if you spot any errors in my code (is there a better way
of doing things?)

Thanks

Tim

Matt Griffith
09-23-2001, 12:46 PM
"Tim Walsha" <tim.walsha@virgin.net> wrote in message
news:3ba9c502$1@news.devx.com...

> I take the point about waiting for a release VB.NET version . It might
help,
> just a bit of a shame because I am sure things should go faster...
>
> Please let me know if you spot any errors in my code (is there a better
way
> of doing things?)

Check out this article on GotDotNet:
http://www.gotdotnet.com/team/windowsforms/windowsformspainting.aspx

It discusses several ways to maximize the performance of GDI+ painting.

If you are painting your own background, pay special attention to
ControlStyles.Opaque.

Other things to keep in mind:

Clean up after yourself. Not disposing of things properly can slow you down.

Be careful where you create expensive objects. Create Pens, Brushes,
Bitmaps, etc. as early as possible. In other words, don't do something like:

g.Drawline(new Pen(Color.Black), x, y, width, height)

inside of loops. Instead reuse such objects as often as possible.

Don't load images from file in your paint handler. (can't tell where you're
loading yours, so this may not apply)

Use regions & clipping rectangles! Paint as little as you can get away with.

Matt Griffith

Jason
09-27-2001, 03:39 PM
Tim, you may want to try DirectX. Okay, this is not "pure .NET," but it works.
You make a COM wrapper for the DirectX 7 or 8 library for VB, then you can
use it directly from C#.

The DirectX SDK contains a couple of very short examples of how to do this
with DirectSound and DirectDraw, but nothing else. The DirectDraw sample
animates a little doggy on a background. It uses double buffering and provides
very fast, flicker-free animation.

This might be overkill for what you are attempting to do, but it is possible
to accomplish what you want with DirectX.


"Tim Walsha" <tim.walsha@virgin.net> wrote:
>
>Thanks for all your responses.
>
>Some of you asked for me to put my code up so here it is.
>
>Let me briefly explain what I am doing.
>
>1. I am using DrawImage (instead of Bitblt) because that is part of the
runtime
>and Bitblt doesn't seem to be supported anymore.
>
>2. DrawImage seems to remove all the hardship which tranditionally was around
>when doing sprite / transparent bitmap operations in VB 6. With Bitblt you
>had to mask out the area where the sprite / graphic was going to be first
>with a sorceAnd. Then you would 'lay over' the actual graphic with an invert.
>This would provide transparent bitmaps or sprites typically found in games
>(ie the edges of the character are transparent and dont rub out the background)
>- takes two operations though in order to draw a sprite to the screen.
>
>3. Addtionally to this overhead you also need to 'double-buffer' or you
get
>screen flicker as the sprites are moved across the screen. Screen flicker
>occurs because you draw the image, pause for a while so the eye can see
it,
>then rub it out, then move the co-ordinates to the next place and start
again.
>Unfortunately if you are drawing a number of graphics the human eye picks
>up the point when you have rubbed the graphics out and the sprites appear
>to flicker when they move. Double buffering is used to draw all these character
>graphics to an off screen surface and then with one big and fast bitblt
you
>move the whole off-screen surface to the main viewing surface.
>
>4. All this seems like overkill - but it works and it provided a really
cool
>animation with no flicker. (Its how all games work - even 3d). An easy way
>to think of this is that you have two boards. One is a construction board
>and the other is a display board.
>
>Here is the point of all this:-
>
>I was trying for ages to find out what the equivalent VB.NET graphics commands
>would be to the VB 6 stuff I mentioned above. I got a lot of help from people
>on this forum and eventually got the following code to work:-
>
>Public BoardGraphics As Graphics 'sets up graphic surface
>Dim BoardBMP as Bitmap 'this is the off screen version
>
>'set the graphics surface to point to the picture box called Board
>Public BoardGraphics As Graphics
>BoardGraphics = Board.CreateGraphics()
>
>'load graphics for object and background up into objects from files
>Character = Image.FromFile("..\Graphics\Telephone.bmp")
>BoardBMP = Image.FromFile("..\Graphics\board.bmp")
>
>
>'set the transparent colour in the bitmap to equal a colour
>Dim TransparentColour As Color
>TransparentColour = Color.Black ' the background to the sprites are black
>Character.MakeTransparent(TransparentColour)
>BoardBMP.MakeTransparent(TransparentColour)
>
>'grab a handle to the boardBMP (where we construct the board off screen)
>HandletoBoardGraphic = Graphics.FromImage(BoardBMP)
>
>'draw character to offscreen surface
>HandletoBoardGraphic.DrawImage(Character , X, Y)
>
>'flip - this is the double buffering bit which stops flicker.
>'it is the last stage of the animation process where we copy the
>'built image over to the actuall screen surface
>BoardGraphics.DrawImage(BoardBMP, 0, 0) ' this flips the image
>
>
>
>Hope this makes sense. The are two problems I found with this method.
>1. Drawimage seems to be an object and as such you can draw over a picturebox
>and it won't destroy whats underneath. (You might think good - but the problem
>stems from the fact that there is significant flicker) When you introduce
>a double buffering flip - the operation transfers the background , but not
>the character because it is logically separate (OO? / framework?)
>
>2. I found that by doing all this work constructing the offscreen surface
>on a bitmap instead of a picturebox (as in the code above) works and by
using
>the 'FromImage' command I can grab all the detail from the offscreen surface
>and transfer it to the actual screen. However it is slow. (compared to the
>long winded but quite quick bitblt VB 6 way of doing things)
>
>I take the point about waiting for a release VB.NET version . It might help,
>just a bit of a shame because I am sure things should go faster...
>
>Please let me know if you spot any errors in my code (is there a better
way
>of doing things?)
>
>Thanks
>
>Tim
>

dan
10-03-2001, 03:08 PM
Tim, you could always use P/Invoke code as you did in the early day.

<dllImport("gdi32",EntryPoint:="BitBlt")> _
Public Shared Function BitBlt(...)

End Function


"Tim " <tim.walsha@virgin.net> wrote:
>
>Hi Guys,
>
>Thanks to everyone who helped with my enquiry about transparent bit maps
>on pictureboxes. I got it working but its so slow. I expect its all the
wrapper
>VB.NET technology thats doing it.
>
>So seeing as I have this code written already in VB ver 6 I thought why
not
>use it.
>
>Basically my orginal code uses BitBlt and Additional Mask graphics to perform
>the transparency. I would like to do this again using VB.NET so as to increase
>the speed back to VB 6 levels. My problem is that when I convert any program
>(or start from scratch) which contains the BitBlt API call VB.NET says it
>doesn't know how to interpret the command.
>
>Has anyone discovered away of using BitBlt (and other GDI stuff) under VB.NET?
>
>If so please advise!
>
>Thanks in advance.
>
>Tim