Masoud Masoud - 2 months ago 18
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);
ms.Close();


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

ms.writeto(Response.outputstream)

Mat Mat
Answer

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