Saving a file with text added to bitmap

jgress

New member
Joined
Oct 12, 2003
Messages
3
Im trying to pull up an existing TIF, add text to it and then save it with a new filename. Ive using part of the Microsft GDIPlus sample as a jumping off point.

I define a brush called backgroundBrush and a bitmap called backgroundImage so the following lines:

backgroundImage = new Bitmap(@"c:\apps\amod.tif");
backgroundBrush = new TextureBrush(backgroundImage);

uses my tif template as a brush that gets painted to the entire client area in the OnPaint method when I do the following:

g.FillRectangle(backgroundBrush, ClientRectangle);

I then add code in the OnPaint method to write text to the "screen"? Im so confused.

string textToDraw="John Smith";
g.DrawString(textToDraw, textFont, new SolidBrush(Color.Black), 184, 342);

textToDraw="Something Else";
g.DrawString(textToDraw, textFont, new SolidBrush(Color.Black), 236, 390);

Finally I try to save it:
backgroundImage.Save(@"c:\apps\saveamod.tif");

The text does not save, only the bitmap.

I understand why this happens, I just dont know what to do about it. Truthfully Id rather not see the image at all, Id just like to create it and save it and be done with it. This is going to be written to run in a batch process mode and It will be data-driven rather than the hard-coded example here. Im worried that once I figure out how to save the text with the image as a new file Ill have to completely rewrite it to do this without it being displayed on the screen.

Any help would be appreciated.

Thank you,

Jeff Gress

Heres the entire code (its short):

public class TextSample : Form
{
private System.ComponentModel.Container components;
private Brush backgroundBrush;
private Font textFont;
private FontFamily serifFontFamily;
public static Image backgroundImage;


public TextSample()
{
InitializeComponent();

serifFontFamily = new FontFamily (GenericFontFamilies.Serif);
this.SetStyle(ControlStyles.ResizeRedraw,true);
backgroundImage = new Bitmap(@"c:\apps\amod.tif");

//Now create the brush we are going to use to paint the background
backgroundBrush = new TextureBrush(backgroundImage);
textFont = new Font(serifFontFamily, 24);
}

protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
g.FillRectangle(backgroundBrush, ClientRectangle);

string textToDraw="John Smith";
g.DrawString(textToDraw, textFont, new SolidBrush(Color.Black), 184, 342);
textToDraw="General Motors";
g.DrawString(textToDraw, textFont, new SolidBrush(Color.Black), 236, 390);
backgroundImage.Save(@"c:\apps\saveamod.tif");
}


private void InitializeComponent()
{
this.components = new System.ComponentModel.Container ();
this.Size = new System.Drawing.Size(800,640);
this.Text = "Test Application";
}


[STAThread]
public static void Main()
{
Application.Run(new TextSample());
}
}
 
where did you create the Graphics object from?
if you want to paint onto a Bitmap, you must do
Code:
Graphics g=Graphics.FromImage(IMAGE);
// do your drawing with the "g" object
IMAGE.Save();

you should create the Graphics FROM the Bitmap you want to paint on.
check if its that :P
 
Thanks, Menge.

Ive completely changed it, thanks to other suggestions as well, so that Im not using the OnPaint method at all now so I dont see the image as its being created, which is what I wanted.

I create an image object, then a bitmap from that and finally a graphics from the bitmap. Does that seem crazy or what?

The graphics object gives me most of my functionality, including the DrawString method, and the bitmap object gives me the SetResolution method, which otherwise defaults to 96,96 I think instead of 200 X 200.

I can use image.save or bitmap.save, but I dont want to use the encoder stuff -- at least none of it that Ive seen -- because Im not using LZW compression of my TIF, I want it to be saves as a group IV TIFF.

The last think I cant work out is how to save it in black and white. It defaults to color. I end up with a 100K tif image that I pull up in XP and change the attributes from color to black and white and my image shrinks to 14K with no visable loss of clarity.

Can anyone tell me how to save this as black and white instead of color? The image Im using as a template is black and white and Im just writing on the surface of the graphics object with a black pen.

Thanks again,

Jeff

textFont = new Font(serifFontFamily, 24);
Image image = Image.FromFile(@"c:\apps\amod2.tif");
Bitmap bitmap = new Bitmap(image);
Graphics g = Graphics.FromImage(bitmap);

g.SmoothingMode = SmoothingMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

string textToDraw="John Smith";
g.DrawString(textToDraw, textFont, new SolidBrush(Color.Black), 184, 342);
textToDraw="General Motors";
g.DrawString(textToDraw, textFont, new SolidBrush(Color.Black), 236, 390);
bitmap.SetResolution(200,200);

bitmap.Save(@"c:\apps\amod3.tif");
 
Back
Top