Reputation: 43
My application in winforms c# and my form need to retrieve the image from database and store in the local machine (Where my application is installed) and viewed in the PictureBox control.
I have the following code (To store image locally and to view in PictureBox control):
//dsProjects is a dataset which is retrieved from database
if (dsProjects.Tables[0].Rows.Count > 0)
{
int idxCurPageJob = (iPageNum * 8);
string path = AppDomain.CurrentDomain.BaseDirectory;
string folder = "TEMP";
string fullpath = string.Empty;
fullpath = Path.Combine(path, folder);
bool exists = System.IO.Directory.Exists(fullpath);
try
{
if (!exists)
Directory.CreateDirectory(folder);
else
{
Directory.Delete(fullpath, true);
Directory.CreateDirectory(folder);
}
}
catch (Exception)
{}
try
{
if (dsProjects.Tables[0].Rows.Count > idxCurPageJob)
{
pnl1.Visible = true;
pnl1.AccessibleName = dsProjects.Tables[0].Rows[idxCurPageJob][1].ToString();
Labelfld1.Text = dsProjects.Tables[0].Rows[idxCurPageJob][0].ToString();
byte[] bgImg = (byte[])dsProjects.Tables[0].Rows[idxCurPageJob][2];
string proCode = dsProjects.Tables[0].Rows[idxCurPageJob][3].ToString();
string strfn = Path.Combine(fullpath, proCode + Convert.ToString(DateTime.Now.ToFileTime()) + idxCurPageJob);
using (FileStream fs = new FileStream(@strfn, FileMode.Create, FileAccess.Write))
{
fs.Write(bgImg, 0, bgImg.Length);
fs.Flush();
fs.Close();
}
picBox1.Image = Image.FromFile(strfn);
picBox1.SizeMode = PictureBoxSizeMode.StretchImage;
picBox1.Refresh();
}
if (dsProjects.Tables[0].Rows.Count > (idxCurPageJob + 1))
{
pnl2.Visible = true;
Labelfld2.Text = dsProjects.Tables[0].Rows[idxCurPageJob + 1][0].ToString();
pnl2.AccessibleName = dsProjects.Tables[0].Rows[idxCurPageJob + 1][1].ToString();
byte[] bgImg = (byte[])dsProjects.Tables[0].Rows[idxCurPageJob + 1][2];
string proCode = dsProjects.Tables[0].Rows[idxCurPageJob + 1][3].ToString();
string strfn = Path.Combine(fullpath, proCode + Convert.ToString(DateTime.Now.ToFileTime()) + (idxCurPageJob + 1));
using (FileStream fs = new FileStream(@strfn, FileMode.Create, FileAccess.Write))
{
fs.Write(bgImg, 0, bgImg.Length);
fs.Flush();
fs.Close();
}
picBox2.Image = Image.FromFile(strfn);
picBox2.SizeMode = PictureBoxSizeMode.StretchImage;
picBox2.Refresh();
}
.....
}
catch (Exception ex)
{
StreamWriter sw;
DateTime dtLogFileCreated = DateTime.Now;
try
{
sw = new StreamWriter("Project Form crash-" + dtLogFileCreated.Day + dtLogFileCreated.Month + dtLogFileCreated.Year + "-" + dtLogFileCreated.Second + dtLogFileCreated.Minute + dtLogFileCreated.Hour + ".txt");
sw.WriteLine("### Server Crash ###");
sw.WriteLine("### Message : ###" + ex.Message + "### StackTrace : ###" + ex.StackTrace + "###Soruce : ###" + ex.Source + "### InnerException : ###" + ex.InnerException + "### Data : ###" + ex.Data + " ### END of LOG ###");
sw.Close();
}
finally
{
Application.Exit();
}
}
}
Afer installing the application in PC and when restarting the PC and trying to open the application then to this form the application get crashed. But after restart the application (Not the PC) I am not facing any issue.
Below is the Error Log details.
### Server Crash :### ### Message : ###Could not find a part of the path 'C:\Program Files\Autoscan Pte Ltd\STK PTA\TEMP\1100101308609520058907460'. ### StackTrace : ### at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) at PTA.Forms.frmPIProject.SetCurrentPageProjects()###Soruce : ###mscorlib### InnerException : ###### Data : ###System.Collections.ListDictionaryInternal ### END of LOG ###
This error is happening in this line of code
using (FileStream fs = new FileStream(@strfn, FileMode.Create, FileAccess.Write))
{
fs.Write(bgImg, 0, bgImg.Length);
fs.Flush();
fs.Close();
}
Really appreciate your help on this
Upvotes: 1
Views: 22843
Reputation: 21
Before doing any changes make sure you have given full permission to the folder and files.
I resolved this issue by turning off one of the settings in my browser. You can disable one of the settings in security tab > custom level > "Include local directory path when uploading files to a server" in IE and then try again.
This settings gets enabled when you add your site to the trusted sites. Either remove your site from trusted list or manually disable this setting and issue will be fixed.
Similarly if you are facing the same issue on other browsers, look for the above setting and mark it as disable and issue will get resolved.
Upvotes: 0
Reputation: 5629
From your code, the fact you call it a "temp" folder, and the fact you never even bother to keep track of the names of the files you create, I kind of doubt you actually have any reason to save the image to disk.
You can just make an Image object straight from the bytes, and assign it to your control:
byte[] bgImg = ???; // your code to fetch the bytes from the DB
Image imgFromDb;
using (MemoryStream ms = new MemoryStream(bgImg))
using (Image tmpImg = Image.FromStream(ms))
{
// Needs to be wrapped in "new Bitmap(Image img)" constructor to avoid
// the image being linked to and depending on the input stream.
imgFromDb = new Bitmap(tmpImg);
}
picBox1.Image = imgFromDb;
Upvotes: 0
Reputation: 27055
Perhaps your directory does not get created.. It's best to not swallow your exceptions. Remove the try / catch from this piece of code and check if the error is here:
if (!exists)
Directory.CreateDirectory(folder);
else
{
Directory.Delete(fullpath, true);
Directory.CreateDirectory(folder);
}
I think your app does not have the right permissions to write in the Program Files folder.
Upvotes: 3