By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,876 Members | 1,291 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,876 IT Pros & Developers. It's quick & easy.

How to get a valid System.Drawing.Image

P: n/a
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=...
}
}
}

Oct 16 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a

Martijn Mulder wrote:
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;
}

Oct 16 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.