Another image problem
-
I read an image file,then I want to write a a string on it and save it again. I write an string with whith graphic object in the Paint event,but I cant save it to a file. I think I don't really write on a image,I just write on the screen,so how to write a text on image an save it? Mazy No sig. available now.
-
I read an image file,then I want to write a a string on it and save it again. I write an string with whith graphic object in the Paint event,but I cant save it to a file. I think I don't really write on a image,I just write on the screen,so how to write a text on image an save it? Mazy No sig. available now.
The following will open an image, create a
Graphics
object to use for drawing to it, draw a string, and save it to another file.Image image = Image.FromFile("source.jpg");
Graphics g = Graphics.FromImage(image);
g.DrawString("Hello World!", new Font("Tahoma", 24), Brushes.Magenta, 10.0f, 10.0f);
g.Dispose();
image.Save("target.jpg");Note that this will only work for non-indexed image formats. For an indexed format you could just create a new non-indexed
Image
into which you first draw the loadedImage
before writing other stuff on top. Of course when you save the file it won't be in the same format as the original.Image image = Image.FromFile("source.gif");
Bitmap bmp = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(image, 0.0f, 0.0f);
g.DrawString("Hello World!", new Font("Tahoma", 24), Brushes.Magenta, 10.0f, 10.0f);
g.Dispose();
bmp.Save("target.jpg"); -
The following will open an image, create a
Graphics
object to use for drawing to it, draw a string, and save it to another file.Image image = Image.FromFile("source.jpg");
Graphics g = Graphics.FromImage(image);
g.DrawString("Hello World!", new Font("Tahoma", 24), Brushes.Magenta, 10.0f, 10.0f);
g.Dispose();
image.Save("target.jpg");Note that this will only work for non-indexed image formats. For an indexed format you could just create a new non-indexed
Image
into which you first draw the loadedImage
before writing other stuff on top. Of course when you save the file it won't be in the same format as the original.Image image = Image.FromFile("source.gif");
Bitmap bmp = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(image, 0.0f, 0.0f);
g.DrawString("Hello World!", new Font("Tahoma", 24), Brushes.Magenta, 10.0f, 10.0f);
g.Dispose();
bmp.Save("target.jpg"); -
The following will open an image, create a
Graphics
object to use for drawing to it, draw a string, and save it to another file.Image image = Image.FromFile("source.jpg");
Graphics g = Graphics.FromImage(image);
g.DrawString("Hello World!", new Font("Tahoma", 24), Brushes.Magenta, 10.0f, 10.0f);
g.Dispose();
image.Save("target.jpg");Note that this will only work for non-indexed image formats. For an indexed format you could just create a new non-indexed
Image
into which you first draw the loadedImage
before writing other stuff on top. Of course when you save the file it won't be in the same format as the original.Image image = Image.FromFile("source.gif");
Bitmap bmp = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(image, 0.0f, 0.0f);
g.DrawString("Hello World!", new Font("Tahoma", 24), Brushes.Magenta, 10.0f, 10.0f);
g.Dispose();
bmp.Save("target.jpg"); -
It works,but I can't use it for tiff file,unhandled error happend for the Save method. Any idea? Mazy No sig. available now.
What was the
Exception
that you got? If you want to save to TIFF format you need to specify an encoder like so:ImageCodecInfo FindEncoder(ImageFormat format) {
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
for (int i = 0; i < encoders.Length; i++)
if (encoders[i].FormatID == format.Guid)
return encoders[i];
return null;
}...
image.Save("target.tiff", FindEncoder(ImageFormat.Tiff), null);
...Actually the example I gave before for saving an index-formatted image probably should have explicity specified the JPEG encoder even though I believe it uses
Image.RawFormat
which defaults to the JPEG format (at least on my system). -
What was the
Exception
that you got? If you want to save to TIFF format you need to specify an encoder like so:ImageCodecInfo FindEncoder(ImageFormat format) {
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
for (int i = 0; i < encoders.Length; i++)
if (encoders[i].FormatID == format.Guid)
return encoders[i];
return null;
}...
image.Save("target.tiff", FindEncoder(ImageFormat.Tiff), null);
...Actually the example I gave before for saving an index-formatted image probably should have explicity specified the JPEG encoder even though I believe it uses
Image.RawFormat
which defaults to the JPEG format (at least on my system).This is my code for multipage Tiff file:
Image image = Image.FromFile("1.tiff");
image.SelectActiveFrame(FrameDimension.Page,0);
Graphics g = Graphics.FromImage(image);
g.DrawString("Hello World!", new Font("Tahoma", 24), Brushes.Magenta, 10.0f, 10.0f);
g.Dispose();
image.Save("1.tiff",FindEncoder(ImageFormat.Tiff),null);This is an error: A Generic Error Ocurde In GDI+ The file is multipage and I want to save it into same file. Mazy No sig. available now.