Drawing on existing image

  • Thread starter Thread starter Christina Androne
  • Start date Start date
C

Christina Androne

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
 
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
 
Bob said:
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?
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 ..
 
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
 
Bob said:
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
 
Back
Top