Click to See Complete Forum and Search --> : [RESOLVED] DrawString problem


bulgarian388
08-26-2007, 07:22 PM
Hi guys, I have a page I'm working on that I want people to be able to query and it does a look up and returns the results as an image. I have everything on it done but the image making part. I keep getting the following error:

"System.NullReferenceException: Object reference not set to an instance of an object."

On this line:

"myGraphics.DrawString("Hello", myFont, myBrush, myPointF);"

This is the first time ever that I'm trying to create an image this way, so I have no idea how to debug it. Any help would be appreciated, here is the code:


// I've removed the irrelevant code, ie db lookup.

using System;
using System.Drawing;

public partial class _Default : System.Web.UI.Page {
// Global C# Objects
public Graphics myGraphics;
public Font myFont = new Font("Verdana", 10);
public SolidBrush myBrush = new SolidBrush(Color.Black);
public PointF myPointF = new PointF(2.0F, 2.0F);

protected void Page_Load(object sender, EventArgs e) {
myGraphics.DrawString("Hello", myFont, myBrush, myPointF);
}
}

Cstick
08-27-2007, 11:14 PM
Hope this helps.


public partial class _Default : System.Web.UI.Page
{
public Graphics g;
public Font f = new Font("Verdana", 10);
public SolidBrush b = new SolidBrush(Color.White);
public Point p = new Point(2, 2);

protected void Page_Load(object sender, EventArgs e)
{
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(100, 100, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
g = Graphics.FromImage(bmp);
g.DrawString("Hello", f, b, p);

Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "image/jpeg";
bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.End();
}
}

bulgarian388
09-10-2007, 03:16 AM
Hi Cstick!

Sorry I didn't reply back since you posted your code, but I got swamped with two projects to I had to put this imaging thing on the back burner for a bit.

Anyway, I tried your code and it works, but now I'm running into an issue of sorts. The text generated is in horrible quality. It's completely pixelated and you can see artifacts around the words.

Is there a way to increase the quality settings when the image is streamed over? I tried looking around the web, but the solutions end up breaking the code, so I'm stuck nowhere.

Any help would be really appreciated. Here is my current code, minus the db lookup:


using System;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;

public partial class _Default : System.Web.UI.Page {
// Global C# Variables

// Global Form Variables

// Global Multi-use Variables

// Global C# Objects
public Bitmap MyBitmap;
public Graphics MyGraphics;
public Font MyFont = new Font("Verdana", 32);
public SolidBrush MyBrush = new SolidBrush(Color.White);
public SolidBrush MyBackColor = new SolidBrush(Color.Green);
public Point MyPoint = new Point(0, 0);

protected void Page_Load(object sender, EventArgs e) {
MyBitmap = new Bitmap(300, 300, PixelFormat.Format32bppRgb);
MyGraphics = Graphics.FromImage(MyBitmap);
MyGraphics.FillRectangle(MyBackColor, 0, 0, 300, 300);
MyGraphics.DrawString("Hello!", MyFont, MyBrush, MyPoint);

Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "image/jpeg";
MyBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
Response.End();
}
}

Cstick
09-10-2007, 08:21 AM
You are creating a jpeg image, which by default compresses the image to some percentage, it appears to be in the 60-70% range, but that is just a guess. Below is the code to create a jpeg image with 100% compression. There is also commented code for creating a gif image, which would also work by swapping the 2 commented lines with your original code. Look at the website below if you want to learn more about what you are doing.

http://www.bobpowell.net/faqmain.htm


public Font MyFont = newFont("Verdana", 32);
public SolidBrush MyBrush = newSolidBrush(Color.White);
public SolidBrush MyBackColor = newSolidBrush(Color.Green);
public Point MyPoint = newPoint(0, 0);


protectedvoid Page_Load(object sender, EventArgs e)


{Bitmap MyBitmap = newBitmap(300, 300, PixelFormat.Format32bppArgb);
Graphics MyGraphics = Graphics.FromImage(MyBitmap);
MyGraphics.FillRectangle(MyBackColor, 0, 0, 300, 300);
MyGraphics.DrawString("Hello!", MyFont, MyBrush, MyPoint);

ImageCodecInfo ici = null;

foreach (ImageCodecInfo codec inImageCodecInfo.GetImageEncoders())


{if (codec.FormatID.Equals(ImageFormat.Jpeg.Guid))
ici = codec;


}

EncoderParameters encoderParams = newEncoderParameters();
encoderParams.Param[0] = newEncoderParameter(Encoder.Quality, (long)100);

Response.ClearContent();
Response.ClearHeaders();

Response.ContentType = ici.MimeType;
MyBitmap.Save(Response.OutputStream, ici, encoderParams);

//Response.ContentType = "image/gif";
//MyBitmap.Save(Response.OutputStream, ImageFormat.Gif);


Response.End();


}

bulgarian388
09-10-2007, 02:01 PM
Cstick, thanks so much for the help you provided. I now have the image working, and looking, perfectly. That website too seems like a very good resource. I only read two articles on it, but I'll definitely reference it in the future for graphical help.

Once again, thanks! :)