Converting a Graphics object to bitmap - please help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • almurph@altavista.com

    Converting a Graphics object to bitmap - please help!

    RE: Tryign to convert Graphics object to a bitmap


    Hi,

    Hope you can help me with this. I have to open a file and add some
    text to it and then display it. So I create an Image object then
    import it into a Graphics object and add the text.
    I then try converting it to a bitmap image using the following
    line:
    Bitmap newBit = new Bitmap(440, 60, gfx);

    but this does not works as when I display it the image does not
    appear.

    Can anyone help me please? Any suggestions/corrections/hints/code-
    samples would be much appreciated.

    Thank you,
    Al.



    **** CODE ****

    Image img = Image.FromFile ("C:\a.jpg") ;
    Font myFont = new Font("Arial", 16);
    SolidBrush myBrush = new SolidBrush(Colo r.Black);

    Graphics gfx=Graphics.Fr omImage(img);
    gfx.DrawString( "Some Data", myFont, myBrush, 100.0F, 30.0F);

    Bitmap newBit = new Bitmap(440, 60, gfx);

    this.pbInputIma ge.Image = newBit ;


    **** END CODE ****
  • Marc Gravell

    #2
    Re: Converting a Graphics object to bitmap - please help!

    You already have an image - img.

    Have you tried this.pbInputIma ge.Image = img;

    Note that you could do with some "using" statements in there:

    // *don't* dispose img; we want to give it to the
    PictureBox
    Image img = Image.FromFile( @"C:\a.jpg") ;
    // but everything else is temp...
    using(Font myFont = new Font("Arial", 16))
    using(SolidBrus h myBrush = new SolidBrush(Colo r.Black))
    using (Graphics gfx = Graphics.FromIm age(img))
    {
    gfx.DrawString( "Some Data", myFont, myBrush, 0, 0);
    }
    this.pbInputIma ge.Image = img;

    Note that if your source image is indexed, you may need to create a
    new Image, create gfx from *that* image, and write both the string and
    the other image (gfx.DrawImage( ...)) to the new image.

    Marc

    Comment

    • Marc Gravell

      #3
      Re: Converting a Graphics object to bitmap - please help!

      Putting this together with your earlier post - something like:
      // resize
      Image newSizeImage;
      using (Image img = Image.FromFile( @"C:\a.jpg") )
      {
      newSizeImage = new Bitmap(img, 440, 60);
      }
      // add text
      using (Font myFont = new Font("Arial", 16))
      using (SolidBrush myBrush = new SolidBrush(Colo r.Black))
      using (Graphics gfx = Graphics.FromIm age(newSizeImag e))
      {
      gfx.DrawString( "Some Data", myFont, myBrush, 0, 0);
      }
      // TODO: now use blah.Image = newSizeImage;

      Comment

      • almurph@altavista.com

        #4
        Re: Converting a Graphics object to bitmap - please help!

        On Feb 20, 2:38 pm, Marc Gravell <marc.grav...@g mail.comwrote:
        Putting this together with your earlier post - something like:
                    // resize
                    Image newSizeImage;
                    using (Image img = Image.FromFile( @"C:\a.jpg") )
                    {
                        newSizeImage = new Bitmap(img, 440, 60);
                    }
                    // add text
                    using (Font myFont = new Font("Arial", 16))
                    using (SolidBrush myBrush = new SolidBrush(Colo r..Black))
                    using (Graphics gfx = Graphics.FromIm age(newSizeImag e))
                    {
                        gfx.DrawString( "Some Data", myFont, myBrush, 0, 0);
                    }
                    // TODO: now use blah.Image = newSizeImage;
        Mark - ur a star - works like a charm now. Thank you very much.

        Oh, btw - I have never see the "using" keyword used in such a fashion.
        Can you explain it perhaps?

        Thank you,
        Al.
        The stupid one.

        Comment

        • Marc Gravell

          #5
          Re: Converting a Graphics object to bitmap - please help!

          Many GDI (graphics) objects are disposable - meaning that they
          implement IDisposable, and should be explicitely released when you are
          done with them to free the (usually unmanaged) resource [probably a
          GDI handle in this case].

          The "using" syntax here is a language shortcut, and

          using (Font myFont = new Font("Arial", 16)) {
          // some code
          }
          is [roughly] identical to:
          Font myFont = new Font("Arial", 16));
          try {
          // some code
          } finally {
          if(myFont!=null ) myFont.Dispose( );
          }

          There are some subtle differences (actually a separate backing
          variable is used etc) but that captures the essense; the main point is
          that regardless of success or failure, the "finally" block ensures
          that the font is disposed. I've also wrapped the brush and the
          graphics object. If you were simply writing to disk, I'd also
          Dispose() the newSizeImage - however, in this example we want
          newSizeImage to live longer than just our method (so that the UI can
          display it!), so we don't dispose this one.

          The rule of thumb is: if you have responsibility for an IDisposable
          object (i.e. you created it etc), then it is your job to Dispose() it
          *when you are sure that you are done*. Many IDisposable objects also
          have a finalizer, so the GC might also release the resource
          eventually, but to be tidy why not release sooner?

          Marc

          Comment

          • almurph@altavista.com

            #6
            Re: Converting a Graphics object to bitmap - please help!

            On Feb 20, 2:52 pm, Marc Gravell <marc.grav...@g mail.comwrote:
            Many GDI (graphics) objects are disposable - meaning that they
            implement IDisposable, and should be explicitely released when you are
            done with them to free the (usually unmanaged) resource [probably a
            GDI handle in this case].
            >
            The "using" syntax here is a language shortcut, and
            >
            using (Font myFont = new Font("Arial", 16)) {
              // some code}
            >
            is [roughly] identical to:
            Font myFont = new Font("Arial", 16));
            try {
              // some code} finally {
            >
              if(myFont!=null ) myFont.Dispose( );
            >
            }
            >
            There are some subtle differences (actually a separate backing
            variable is used etc) but that captures the essense; the main point is
            that regardless of success or failure, the "finally" block ensures
            that the font is disposed. I've also wrapped the brush and the
            graphics object. If you were simply writing to disk, I'd also
            Dispose() the newSizeImage - however, in this example we want
            newSizeImage to live longer than just our method (so that the UI can
            display it!), so we don't dispose this one.
            >
            The rule of thumb is: if you have responsibility for an IDisposable
            object (i.e. you created it etc), then it is your job to Dispose() it
            *when you are sure that you are done*. Many IDisposable objects also
            have a finalizer, so the GC might also release the resource
            eventually, but to be tidy why not release sooner?
            >
            Marc
            Marc - its been an education. Thank you very much sir.

            Al.

            Comment

            Working...