How to get a valid System.Drawing.Image

  • Thread starter Thread starter Martijn Mulder
  • Start date Start date
M

Martijn Mulder

At startup my application loads an image from a file from disk. If the file
is not there, I still need a valid System.Drawing.Image object but I don't
know how to get one.


//class MyImage
class MyImage
{

//data member image
System.Drawing.Image image;

//constructor
public MyImage():this(@"c:\Some Directory\Some Image.jpg"){}
public MyImage(string a)
{
try
{
image=System.Drawing.Image.FromFile(a);
}
catch
{
image=...
}
}
}
 
Martijn said:
At startup my application loads an image from a file from disk. If the file
is not there, I still need a valid System.Drawing.Image object but I don't
know how to get one.


//class MyImage
class MyImage
{

//data member image
System.Drawing.Image image;

//constructor
public MyImage():this(@"c:\Some Directory\Some Image.jpg"){}
public MyImage(string a)
{
try
{
image=System.Drawing.Image.FromFile(a);
}
catch
{
image=...
}
}
}

I do this:

/// <summary>
/// Returns the image to be displayed if there is no business object
image for this
/// type of business object.
/// </summary>
/// <param name="filePath">The file path for the image file that is not
present,
/// and for which this method should build a "no image"
message.</param>
/// <returns>An image that tells the user that there is no image
available.</returns>
private Image BuildNoImageImage(string filePath)
{
// Buld a standard-size bitmap that will then be scaled later
Bitmap none = new Bitmap(480, 150);
Graphics g = Graphics.FromImage(none);
FontFamily ffam = new FontFamily(GenericFontFamilies.SansSerif);
Font f1 = new Font(ffam, 36.0f);
int x = 20;
int y = 20;
g.DrawString("No image available", f1, Brushes.Black, x, y);
Font f2 = new Font(ffam, 18.0f);
x = 20;
y += 70;
g.DrawString("File: " + filePath, f2, Brushes.Black, x, y);

ffam.Dispose();
f1.Dispose();
f2.Dispose();
return none;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top