Loading a PictureControl from a gif file in C#

H

Hugh Creswell

Hi there - I hope someone can help with this problem....

A colleague and myself are trying to load a PictureControl (using C#)
from a GIF file. We don't want to set the file in the resource
properties, as the file is generated on the fly, and placingthe
filename in the properties appears to embed the file in the resources,
hence not changig unles we re-compile.

The GIF file has been creating using the Excel export method.
The code we are using to load the file is :-

this.pictureBox_DefectCountView.Image =
System.Drawing.Image.FromFile("count.gif");


The file exists, and is openable by other apps - MSpaint, PSP, etc
etc.

However, whenever we call that line of code, it throws an exception,
"Invalid Parameter".

We have tried variations on a theme, such as giving absolute paths,
loading as a BMP file. We have also tried creating a new Bitmap using
:-

Bitamp theBmp = new Bitmap("count.gif");

This also throws the exception.

I've done a google for "C# FromFile Gif Bitmap" and had a look at the
result, and they all seem to suggest that we are trying to do the
right thing...

Has anybody any ideas?

Thanks in advance
 
B

Bob Powell [MVP]

Can you post a *small* image generated by the same method please. Also, how
big (in pixels) is the file that fails and what is the exception thrown?

What you're doing should work. I suspect something odd in the file format or
a huge file.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
M

Matt Berther

Hello Hugh,

Are you sure the Current Directory is correct?

Try hardcoding the full path to count.gif and see if that works....
 
H

Hugh Creswell

Bob
Thanks for the reply. I am unable to post the gif here, so I have
emailed it to you - I hope that is ok... (assuming I have your email
address correctly :) )
 
B

Bob Powell [MVP]

Hello Hugh,
I got your GIF and this works just fine for me. I see your graph in the
picturebox.

private void Form1_Load(object sender, System.EventArgs e)

{

this.pictureBox1.Image=Image.FromFile(@"C:\count.gif");

}



There's nothing wrong with the image AFAICS

What is the exception thrown and the stack-dump provided?

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
H

Hugh Creswell

Bob Powell said:
Hello Hugh,
I got your GIF and this works just fine for me. I see your graph in the
picturebox.

private void Form1_Load(object sender, System.EventArgs e)

{

this.pictureBox1.Image=Image.FromFile(@"C:\count.gif");

}

Bob,

Thanks once again for your help - I have now fixed the problem (it was
related to file paths\names) and it works fine!

Hugh Creswell MBCS MIEE CITP
 

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

Top