-
Sep 10th, 2006, 08:52 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Using BitBlt in VB.NET?
Hi all,
I'm new to bitblt, i have worked on a vb6 project with a partner who used it before, but after trying to apply the methods to vb.net i discovered that vb.net doesn't allow you to blt an image which isn't visible on the screen! in vb6 an invisible picturebox used to do the trick.
I have found some answers from googling, involving methods such as CreateCompatibleDC, SelectObject and others.. but these answers are not well explained and i'm a bit lost as to how to get it to work.
Bsically all i need is an explanation of how to use a .net bitmap or image object and draw it using bitblt. I am wanting to use bitblt to get some better performance then the frameworks functions will provide, but without going all the way to directx.
If anyone has tackled this problem before any help is appreciated, thanks.
-
Sep 10th, 2006, 09:40 AM
#2
Re: Using BitBlt in VB.NET?
The .NET functions wrap most of the API functions anyway. Functions, for example, DrawEllipse:
PHP Code:
Graphics g = this.CreateGraphics();
g.DrawEllipse(Pens.Blue, 0, 0, 50, 50);
..use the DrawEllipse API. Almost all graphics functions make use of the API, so I would suggest learning those. Of course, not all things use the API, so people do often go back to using the API's in .NET. Not sure how to do it in VB.NET, but it can't be much different than it already is. In C# its alot different, but yeah...google is your friend (search the forums too )
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 10th, 2006, 10:21 AM
#3
Thread Starter
Frenzied Member
Re: Using BitBlt in VB.NET?
Thanks chem,
I am quite familiar with gdi+ provided within the framework, you can do great effects with it, however this is a personal project a friend and myself are doing which is a small game, unfortunetly the graphics class methods are too slow to make even a fairly tame game run anywhere near 30fps.
I have been led to believe that blting directly is faster than the .drawimage method (which i can beleive because it seems to run well in vb6) but if that's not the case i'll have to pull my finger out n learn directx.
as for it not being different, well it isn't, if i put my image into a visible picturebox, i can freely blt it into my main picturebox.. BUT make it invisible and it simply captures the part of the screen it was meant to be in (this wasnt a problem in vb6), i need help with using the image object with bltbit without a picturebox control, the examples are a little overwhelming.
I'm sure JR will chime in soon with 101 DX tutorials for me :P
-
Sep 10th, 2006, 10:26 AM
#4
Fanatic Member
Re: Using BitBlt in VB.NET?
If you already have a .NET Bitmap or Image object them painting it on the screen is easy. You just need a Graphics object. If you're painting outside the Paint event, then first make sure you're on the same thread that's running the form, and call CreateGraphics() to return the object. In the paint event it's passed as the event args through e.Graphics. Graphics has a DrawImage method to do just what you want. If flickering is a problem then when the form (or user control if you're drawing in that) loads call SetStyle(ControlStyles.[something about double buffering], True). There are also other styles that can help improve performance.
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Sep 10th, 2006, 10:39 AM
#5
Fanatic Member
Re: Using BitBlt in VB.NET?
You'll need LoadImage to get a HANDLE to the bitmap. CreateCompatibleDC to create an offscreen device context to place the bitmap on. SelectObject to place the bitmap onto the DC. BitBlt to transfer the bits from the offscreen DC of your bitmap to the onscreen DC of the window. And don't forget DeleteDC to release your offscreen DC and DeleteObject to release the bitmap. Google can provide all the prototypes and documentation on msdn needed to use these functions.
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Sep 10th, 2006, 12:48 PM
#6
Re: Using BitBlt in VB.NET?
Originally Posted by Phill64
BUT make it invisible and it simply captures the part of the screen it was meant to be in (this wasnt a problem in vb6), i need help with using the image object with bltbit without a picturebox control, the examples are a little overwhelming.
I think you were blitting it wrong. This is how I would do it in C#:
PHP Code:
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth,
int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);
public enum TernaryRasterOperations
{
SRCCOPY = 0x00CC0020, /* dest = source*/
SRCPAINT = 0x00EE0086, /* dest = source OR dest*/
SRCAND = 0x008800C6, /* dest = source AND dest*/
SRCINVERT = 0x00660046, /* dest = source XOR dest*/
SRCERASE = 0x00440328, /* dest = source AND (NOT dest )*/
NOTSRCCOPY = 0x00330008, /* dest = (NOT source)*/
NOTSRCERASE = 0x001100A6, /* dest = (NOT src) AND (NOT dest) */
MERGECOPY = 0x00C000CA, /* dest = (source AND pattern)*/
MERGEPAINT = 0x00BB0226, /* dest = (NOT source) OR dest*/
PATCOPY = 0x00F00021, /* dest = pattern*/
PATPAINT = 0x00FB0A09, /* dest = DPSnoo*/
PATINVERT = 0x005A0049, /* dest = pattern XOR dest*/
DSTINVERT = 0x00550009, /* dest = (NOT dest)*/
BLACKNESS = 0x00000042, /* dest = BLACK*/
WHITENESS = 0x00FF0062, /* dest = WHITE*/
};
[DllImport("gdi32.dll", ExactSpelling = true,
SetLastError = true)]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll", ExactSpelling = true,
SetLastError = true)]
public static extern bool DeleteDC(IntPtr hdc);
[DllImport("gdi32.dll", ExactSpelling = true)]
public static extern IntPtr SelectObject(IntPtr hDC,
IntPtr hObject);
private void BlitTest()
{
Graphics g = this.CreateGraphics();
IntPtr gDc = g.GetHdc();
IntPtr pSource = CreateCompatibleDC(gDc);
SelectObject(pSource, ((Bitmap)this.pictureBox1.Image).GetHbitmap());
BitBlt(gDc, 0, 0, pictureBox1.Width, pictureBox1.Height, pSource, 0, 0, TernaryRasterOperations.SRCCOPY);
DeleteDC(pSource);
g.ReleaseHdc(gDc);
g.Dispose();
}
...and that works fine with the picturebox invisible. You should be able to convert that to VB.NET, as its all pretty much the same. It blits the picturebox image into the form (the image I used was big, so I had to test it invisible from the start (im lazy I don't resize my forms when I'm testing something )).
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 11th, 2006, 02:15 AM
#7
Thread Starter
Frenzied Member
Re: Using BitBlt in VB.NET?
Thanks chem, that's exactly what i was after, your example is easy to understand.
Last night though i discovered SDL.NET, a simple interface for OpenGL and have been working with that instead, 2d blitting and sprites are quite easy to work with.
However i'm sure ill be wanting bitblt for other projects so im tucking your example away for later.
Thanks again for the reply.
-
Nov 7th, 2006, 11:42 AM
#8
Frenzied Member
Re: [RESOLVED] Using BitBlt in VB.NET?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|