Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Another image problem

Another image problem

Scheduled Pinned Locked Moved C#
helptutorialquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mazdak
    wrote on last edited by
    #1

    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.

    K 1 Reply Last reply
    0
    • M Mazdak

      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.

      K Offline
      K Offline
      Kastro
      wrote on last edited by
      #2

      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 loaded Image 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");

      M 2 Replies Last reply
      0
      • K Kastro

        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 loaded Image 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");

        M Offline
        M Offline
        Mazdak
        wrote on last edited by
        #3

        Thank you. I'll test it very soon. :) Mazy No sig. available now.

        1 Reply Last reply
        0
        • K Kastro

          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 loaded Image 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");

          M Offline
          M Offline
          Mazdak
          wrote on last edited by
          #4

          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.

          K 1 Reply Last reply
          0
          • M Mazdak

            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.

            K Offline
            K Offline
            Kastro
            wrote on last edited by
            #5

            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).

            M 1 Reply Last reply
            0
            • K Kastro

              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).

              M Offline
              M Offline
              Mazdak
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups