0

i have used below code for showing image in image picture box, now i would like add code for opening file (pdf,image,...) after reading from database

            int imageID = Convert.ToInt32(imageIDComboBox.Text);

            // read image bytes from the database and display in picture box
            Byte[] imageByteArray = ProductDB.ReadImage(imageID);
            MemoryStream ms = new MemoryStream(imageByteArray);

            imagePictureBox.Image = System.Drawing.Image.FromStream(ms);
            ms.Close();

i have tried to used below code but it does not recognize Response.

   ms.writeto(Response.outputstream)
Masoud
  • 223
  • 2
  • 13
  • probabbly a duplicate http://stackoverflow.com/questions/8624071/save-and-load-memorystream-to-from-a-file – Mat Sep 22 '16 at 13:47

1 Answers1

1

You need to Save the File somewhere. I suggest you use the GetTempPath method to obtain a temp file name.

after you have saved the file you can open it with the default program of the machine by using the Process class

some pseudo code:

string fileName = "C:\temp\foo.pdf"; //or use Path.GetTempPath()
ms.Write(new StreamWriter(filename)); //you may want to use a using statement for your file stream to ensure the file is closed
Process.Start(filename);

https://msdn.microsoft.com/en-us/library/system.io.path.gettemppath(v=vs.110).aspx https://www.dotnetperls.com/process

EDIT

It seems my pseudo code does not work correctly ;-) here's another snippet:

    Byte[] imageByteArray = ProductDB.ReadImage(imageID);
    string fileName = Path.GetTempPath();

    File.WriteAllBytes(fileName, imageByteArray);

    Process.Start(fileName);
Mat
  • 1,960
  • 5
  • 25
  • 38
  • thanks, i got error "No overload for method 'Write' takes 1 arguments", please help what is the problem, also variable "filename"and "fileName" are not same. – Masoud Sep 22 '16 at 14:13