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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Writing text to images

Writing text to images

Scheduled Pinned Locked Moved C#
graphicsquestion
4 Posts 3 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.
  • A Offline
    A Offline
    adi rusu
    wrote on last edited by
    #1

    Hello. I am trying to write the text given by the user onto an image and save the image. If i save it once using the Save method of the Bitmap object, it works. But if i try again, i get an exception because the file is locked. I have to restart the application to save the file again. Does anybody know a solution? Thanks

    J 1 Reply Last reply
    0
    • A adi rusu

      Hello. I am trying to write the text given by the user onto an image and save the image. If i save it once using the Save method of the Bitmap object, it works. But if i try again, i get an exception because the file is locked. I have to restart the application to save the file again. Does anybody know a solution? Thanks

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      Yes, make sure you Dispose of any Bitmap or File you are using!

      A 1 Reply Last reply
      0
      • J J4amieC

        Yes, make sure you Dispose of any Bitmap or File you are using!

        A Offline
        A Offline
        adi rusu
        wrote on last edited by
        #3

        I did that. Here is my code: Font f = new Font("Arial", 48); Bitmap b = new Bitmap("some path"); Graphics g = Graphics.FromImage(b); g.DrawString(textBox.Text, f, Brushes.White, 50, 50); g.Flush(); try { b.Save("result path", ImageFormat.Jpeg); b.Dispose(); b = null; pictureBox.Image = Image.FromFile("result path"); } catch (Exception ex) { MessageBox.Show(ex.Message); //Displays "A generic error occured in GDI+" } I wrote the message on the forum after i tried this code. So Dispose doesn't really work as I had expected. Any other ideas?

        L 1 Reply Last reply
        0
        • A adi rusu

          I did that. Here is my code: Font f = new Font("Arial", 48); Bitmap b = new Bitmap("some path"); Graphics g = Graphics.FromImage(b); g.DrawString(textBox.Text, f, Brushes.White, 50, 50); g.Flush(); try { b.Save("result path", ImageFormat.Jpeg); b.Dispose(); b = null; pictureBox.Image = Image.FromFile("result path"); } catch (Exception ex) { MessageBox.Show(ex.Message); //Displays "A generic error occured in GDI+" } I wrote the message on the forum after i tried this code. So Dispose doesn't really work as I had expected. Any other ideas?

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, images that get created from a file, lock that file for as long as the Image exists. This holds true for Bitmap b=new Bitmap(path) as well as pictureBox.Image = Image.FromFile(path). As long as your PictureBox is showing an image, loaded from a file like that, you won't be able to delete or modify that file. There are basically two solutions: 1. load the image, copy it to a second image immediately (as in new Bitmap(image)), and dispose of the first image; 2. create a FileStream to the file, and load the image using FromStream, this does not lock the file that your stream refers to.

          FileStream stream=new FileStream(fileSpec, FileMode.Open, FileAccess.Read);
          Image localImage2=Image.FromStream(stream);
          

          :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets


          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