By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,289 Members | 1,375 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,289 IT Pros & Developers. It's quick & easy.

Drawing on existing image

P: n/a
Hi

I want to allow the user to drow some locators on an image and connect
the locators with lines. So I tried to put the following code in the
onmousedown enevt of the picture:
Graphics LGraphics = Graphics.FromImage(imgMap.Image);
LGraphics.DrawString("X", new Font("Arial", 12),
SystemBrushes.WindowText, AX, AY);
Pen LPen = new Pen(System.Drawing.Color.Black);
LPen.Brush = SystemBrushes.WindowText;
if ((FLastX >= 0) && (FLastY >= 0))
{
LGraphics.DrawLine(LPen, FLastX, FLastY, AX, AY);
}
LGraphics.Save();
FLastX = AX;
FLastY = AY;

The really annoying thing is
1) I can't use this code for indexed bitmaps
2) Nothing appears on the image

My questions are:
1) How do I set the color for the text to be shown ("X" in my case)?
2) How should I draw on all types of bitmaps?
and of course
3) What to do so my text and my lines become visible on the bitmap?
Note: imgMap.Image is of type System.Drawing.Image and loaded with a
bitmap.

Thanks in advance for any help !

Christina Androne
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
> The really annoying thing is
1) I can't use this code for indexed bitmaps You can only get a graphics object for a 24 or 32 bit image.
2) Nothing appears on the image It does, you just don't see it.

My questions are:
1) How do I set the color for the text to be shown ("X" in my case)? You've done it correctly
2) How should I draw on all types of bitmaps? You'll have to determine if an image is indexed, if it is, create a non
indexed image of the same size and draw the image followed by the text to
that image. Then, you can save to an indexed image later if you wish.
and of course
3) What to do so my text and my lines become visible on the bitmap?
Graphics.Save has nothing to do with saving the image.

First refresh the picture (I assume you're using picturebox so
pictureBox1.Invalidate() will do that. Then you can save the bitmap such as
imgmap.Image.Save(<filename>,<fileFormat>).

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

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

Read my Blog at http://bobpowelldotnet.blogspot.com

"Christina Androne" <ac********@NotAtHome.ro> wrote in message
news:uV**************@tk2msftngp13.phx.gbl... Hi

I want to allow the user to drow some locators on an image and connect
the locators with lines. So I tried to put the following code in the
onmousedown enevt of the picture:
Graphics LGraphics = Graphics.FromImage(imgMap.Image);
LGraphics.DrawString("X", new Font("Arial", 12),
SystemBrushes.WindowText, AX, AY);
Pen LPen = new Pen(System.Drawing.Color.Black);
LPen.Brush = SystemBrushes.WindowText;
if ((FLastX >= 0) && (FLastY >= 0))
{
LGraphics.DrawLine(LPen, FLastX, FLastY, AX, AY);
}
LGraphics.Save();
FLastX = AX;
FLastY = AY;

The really annoying thing is
1) I can't use this code for indexed bitmaps
2) Nothing appears on the image

My questions are:
1) How do I set the color for the text to be shown ("X" in my case)?
2) How should I draw on all types of bitmaps?
and of course
3) What to do so my text and my lines become visible on the bitmap?
Note: imgMap.Image is of type System.Drawing.Image and loaded with a
bitmap.

Thanks in advance for any help !

Christina Androne

Nov 15 '05 #2

P: n/a
Bob Powell [MVP] wrote:
2) How should I draw on all types of bitmaps? You'll have to determine if an image is indexed, if it is, create a
non indexed image of the same size and draw the image followed by the
text to that image. Then, you can save to an indexed image later if
you wish.


Can you please tell me how do I create the nonindexed image ? Can I do
this programatically or I have to use an image editor?
and of course
3) What to do so my text and my lines become visible on the bitmap?


First refresh the picture (I assume you're using picturebox so
pictureBox1.Invalidate() will do that. Then you can save the bitmap
such as imgmap.Image.Save(<filename>,<fileFormat>).


Well, I am not using picture box ... and I can't save the bitmap to a
file. Is there any way to refresh a bitmap ... if that makes sense ..

Nov 15 '05 #3

P: n/a
You can create the non indexed image like this..

Bitmap bm1=(Bitmap)Image.FromFile("doodaa.gif");
Bitmap bm2=new Bitmap(bm1.Width,bm1.Height);
Graphics g=Graphics.FromImage(bm2);
g.DrawImageUnscaled(bm1,0,0);
//bm2 now contains a non-indexed version of the image.
//Now draw the X..
g.DrawLine(blah-blah....);
g.DrawLine(blah-blah....);
//get rid of the graphics
g.Dispose();
//and save a new gif
bm2.Save("foobar.gif",ImageFormat.Gif);

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

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

Read my Blog at http://bobpowelldotnet.blogspot.com

"Christina Androne" <ac********@NotAtHome.ro> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Bob Powell [MVP] wrote:
2) How should I draw on all types of bitmaps?

You'll have to determine if an image is indexed, if it is, create a
non indexed image of the same size and draw the image followed by the
text to that image. Then, you can save to an indexed image later if
you wish.


Can you please tell me how do I create the nonindexed image ? Can I do
this programatically or I have to use an image editor?
and of course
3) What to do so my text and my lines become visible on the bitmap?


First refresh the picture (I assume you're using picturebox so
pictureBox1.Invalidate() will do that. Then you can save the bitmap
such as imgmap.Image.Save(<filename>,<fileFormat>).


Well, I am not using picture box ... and I can't save the bitmap to a
file. Is there any way to refresh a bitmap ... if that makes sense ..

Nov 15 '05 #4

P: n/a
Bob Powell [MVP] wrote:
You can create the non indexed image like this..

Bitmap bm1=(Bitmap)Image.FromFile("doodaa.gif");
Bitmap bm2=new Bitmap(bm1.Width,bm1.Height);
Graphics g=Graphics.FromImage(bm2);
g.DrawImageUnscaled(bm1,0,0);
//bm2 now contains a non-indexed version of the image.
//Now draw the X..
g.DrawLine(blah-blah....);
g.DrawLine(blah-blah....);
//get rid of the graphics
g.Dispose();
//and save a new gif
bm2.Save("foobar.gif",ImageFormat.Gif);


Thank you,

Christina Androne
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.