views:

519

answers:

1

here is the code i'm using, i really need to make it so the text does not appear inside of a box. is that possible?

int width = (int)g.MeasureString(line, f).Width;
int height = (int)g.MeasureString(line,f).Height;
b = new Bitmap(b, new Size(width, height));
g = Graphics.FromImage(b);

g.Clear(Color.Empty);
g.DrawString(line,f, new SolidBrush(Color.Black), 0, 0);
b.Save(savepoint+line+".tif", System.Drawing.Imaging.ImageFormat.Tiff); 
g.Flush();

What i mean is there can be no Rectangle around the text that is converted to image. So I need to watch it to the same color to create the illusion there is no box, or to never write out that rectangle period.

+1  A: 

Use the color Transparent for the backgtround and a file format that supports transparency, like PNG:

var measure = g.MeasureString(line, f);
int width = (int)measure.Width;
int height = (int)measure.Height;
using (Bitmap b = new Bitmap(width, height)) {
  using (Graphics bg = Graphics.FromImage(b)) {
    bg.Clear(Color.Transparent);
    using (Brush black = new SolidBrush(Color.Black)) {
      bg.DrawString(line, f, black, 0, 0);
    }
  }
  b.Save(savepoint+line+".png", System.Drawing.Imaging.ImageFormat.Png); 
}

I notice that you did overwrite your Graphics instance, didn't dispose the objects that you create, and called Graphics.Flush for no apparent reason...

Guffa
And don't forget to properly parent the control. The "Transparent" background of a string really just sets the background to the Parent's.
Chad Stewart
keeps coming back with parameter not valid at using (b = new Bitmap(b, new Size(width, height)))
Mike
even otuside of the loop it still places a box around the text
Mike
@Mike: Where do you get the original graphics object from (variable g)? What loop are you talking about?
Guffa
I do a foreach to get the line. but there is still a blackbox being draw by the DrawString, the black outine of the text is the problem.
Mike
@Mike: The black box has to come from somewhere else. I tried the code, and as expected it doesn't draw any box around the text. How are you using the image?
Guffa