Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String on a Bitmap

Status
Not open for further replies.

seanbo

Programmer
Jun 6, 2003
407
0
0
GB
how can i add a string to a Bitmap object?
 
Do you mean that you want one object, like MyExtendedBitmap, that has a Bitmap and a string in it?
 
no, i mean changing various pixels of a pitmap so that you are left with the appearence of text. i solved the problem myself late last night.

i'm just gonna nip to the garage to get my breaks fixed. when i get back, i'll post my solution.
 
Here is my solution as promised:

first of all i used gdi32.dll, so lets stick this piece of code in somewhere...

[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);

...i can use BitBlt to copy from a Graphics object to a Bitmap. hurrah, now i can use DrawString. now i need a graphics object that can be any size i choose, and that won't be drawn on screen...

Bitmap bm = new Bitmap(p.pageWidth, p.pageHeight);
IntPtr hbm = bm.GetHbitmap();
bob = Image.FromHbitmap(hbm);
Graphics g = Graphics.FromImage(bob);


...there's probably a better way of getting this graphics object, but i'm gonna use hbm again soon so i think that justifies it. the next step is to diddle with the graphics object - i drew a string. to finish with, i have to copy Graphics g to bob...

IntPtr hdc = g.GetHdc();
BitBlt(hbm, 0, 0, bob.Width, bob.Height, hdc, 0, 0, 13369376);
g.ReleaseHdc(hdc);

...and that's it. spanky. incedentaly, bob is short for 'Blitter OBject', another term for 'raster garphic'.

i think the routine is pretty tight, but i'd like to hear people ideas for bettering it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top