Masoud Masoud - 1 month ago 4x
C# Question

how to open file after read image bytes from the database

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);

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


Mat Mat

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


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);