0

I have created a button where it can upload all the pic files as well as the doc files and the PDF files in the system. Here is the code for the following:

        if (dlg.ShowDialog() == DialogResult.OK)
        {

            pictureBox2.Image = Image.FromFile(dlg.FileName);

            pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
            currentFileName = dlg.FileName;
            button2.Enabled = true;

        }

    }

But i have a error poping out when i want upload a doc files. First of all is it possible to upload a doc file? if yes, then i have a issues showing out of memory in the following line of code

pictureBox2.Image = Image.FromFile(dlg.FileName);
Kami
  • 19,134
  • 4
  • 51
  • 63
  • 3
    This code appears to load an image to a picture box. Nothing to do with uploading. Can you clarify what it is you are trying to do? – Kami Oct 07 '13 at 09:23
  • [See the docs for `Image.FromFile`](http://msdn.microsoft.com/en-us/library/stf701f5.aspx) – Ahmed KRAIEM Oct 07 '13 at 09:25

4 Answers4

0

PictureBox control is used only for showing images in a WinForm application (take a look at MSDN). To show .doc file contents in your application you have to use word or some workaround (like posted here)

Community
  • 1
  • 1
gzaxx
  • 17,312
  • 2
  • 36
  • 54
0

Please take reference at the link from MSDN. It will throw OutOfmemoryException when you loading a picture have not appropriate format.

To resolve your problem you should check format of the picture file more than loading directly as above.

Please reference at here to know how can you detect format of an image file.

To loading PDF or Word document you should take reference here.

Community
  • 1
  • 1
Toan Vo
  • 1,270
  • 9
  • 19
0

The reason why you get that error is because a doc file is not a valid image format.

This is outlined in the documentation: http://msdn.microsoft.com/en-us/library/stf701f5.aspx

it is possible to upload a doc file but not in the context you want, i.e. using Image.FromFile and assign it to a picture box object.

Darren
  • 68,902
  • 24
  • 138
  • 144
0

http://msdn.microsoft.com/en-us/library/stf701f5.aspx

Out of Memory exception covered in that topic.

The FromFile method will throw an exception if the file type is invalid.

You should do necessary checks on the file type's compatibility first, not to mention wrap a try catch around this method to ensure you're coding as defensively as possible.