|
-
May 25th, 2001, 10:31 AM
#1
Thread Starter
Junior Member
Draw text on BMPs without DC
I need to know if it's possible to draw text or some string on a bitmap without the use of device contexts, using available API methods ... Think hard, I'm sure it's possible.
And by the way, all this in VB, because I know it's possible in VB.NET but I don't want it.
Thanx in advance.
-
May 25th, 2001, 11:46 AM
#2
PowerPoster
What do you mean <bitmaps without DC>?
like if you're using a imagebox?
-
May 25th, 2001, 05:11 PM
#3
Addicted Member
As far as I know, you can't.
Most GDI APIs use the hDC parameter, and if you want to draw something on the bitmap you must attach it to a DC, so...
-
May 26th, 2001, 10:49 AM
#4
PowerPoster
How abt this? No DC but pictureBox already take care of it for oyu.
Code:
Picture1.Print "MyText"
-
May 28th, 2001, 04:46 PM
#5
Addicted Member
It still draws the text on a DC.
PictureBox has its own device context. It's the hDC property
-
May 28th, 2001, 05:28 PM
#6
New Member
It depends what you mean by a 'Bitmap'. It could mean a .BMP file on disk, an image loaded into a picture box or other graphical control or an API Bitmap object.
To start with though I don't understand why you don't want to use DC's, they would make the entire process a LOT easier.. If your just not comfortable with working with DC's then simply use the .CurrentX and .CurrentY properties of a picture box to position the text and the .Print() method to draw the text (I know it doesn't appear on the pictures box's list of methods, but it's there) As pointed out in an earlier post this does in-fact make use of hDC's behind the scenes.
To render text with absolutely no hDC's then you'll have to draw it on yourself - Get the image into a byte array then set the bytes where the text needs to be.
When working off disk the first part is dead easy, simply open the file in binary mode, read the headers (And palette if < 15-bit), create a byte array the right size depending on the height, width and bit depth of the image then read the image data in. See Wotsit for more information on the format.
If you’re working with a bitmap object or picture box (Which ironically is already using a number of hDC's...) then you have to get access to the image data. Normally you'd do this with GetDIBits() but that requires an hDC so we have to use a hack.. You can create an empty byte array and copy the array descriptor from the bitmap object (The .Image.Handle property of the picture box will give you the bitmap handle) to your array using the RtlMoveMemory() API. Once there then it 'points' to the bitmap data from the picture box, make sure you remove the descriptor after finishing with the image though, otherwise you'll have nasty problems on NT machines. There are examples of this hack on vbAccelerator in the DIB class or the DIBSection class on my site by Warren Galyen.
Once you've got your byte array then you'll need to draw the text. How you get the 'shape' of the text is a problem you'll have to solve, personally I'd render the text to a temporary DC using the DrawText() or TextOut() API's then draw it on the bitmap, but no DC's... You may want to create a bitmap of your font then read that in and copy the chunks onto your image; many skinned applications do exactly this. However you decide to do it you'll need to take into account that the letters are not rectangular and so only draw the area's that have the letter on, not the area's that are simply the background (Unless you want text with a solid background).
If you’re using anti-aliased text then you'll need to blend the edges into the underlying image. To do this use a linear interpolation routine, there's an implementation of this in the pixel library on my page.
If your text is aliased but you want to anti-alias it then you'll need to render the bitmap font larger than you'll need it when you draw it, then sub-sample down to get an alpha-map. Take that into account when drawing your text, again using the LinearB() function.
Finally save the image back to disk if your using that method or clean up your array and your done.. phew..
Or you could just use the .Print() method and save yourself about a weeks work and lots of headaches 
Hope this helps,
Mike
-- EDais --
WWW: Http://Members.xoom.com/EDais/
Work E-Mail: [email protected]
Other E-Mail: [email protected]
-
Jun 7th, 2001, 01:47 PM
#7
Thread Starter
Junior Member
Follow-up ...
I think definitively that I haven't been quite clear ...
What I wanted is to attach a bitmap to a DC, and then draw on it, text, shapes, etc. But I could only (at this time) do so with an existing DC, because each time I attempted to create a new DC it wouldn't work ... Finally I figured out to handle offscreen DC and solve my problem ... Thanks anyway
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
|