1

我在 winforms c# 中的应用程序和我的表单需要从数据库中检索图像并存储在本地机器(我的应用程序安装在哪里)并在 PictureBox 控件中查看。

我有以下代码(在本地存储图像并在 PictureBox 控件中查看):

//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();
        }
    }
}

在 PC 中安装应用程序并重新启动 PC 并尝试打开应用程序时,应用程序会以这种形式崩溃。但是在重新启动应用程序(不是 PC)后,我没有遇到任何问题。

以下是错误日志详细信息。

### 服务器崩溃:###  
### 消息:###找不到路径“C:\Program Files\Autoscan Pte Ltd\STK PTA\TEMP\1100101308609520058907460”的一部分。  
### StackTrace : ### 在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)  
   在 System.IO.FileStream.Init(字符串路径,FileMode 模式,FileAccess 访问,Int32 权限,布尔用户权限,文件共享共享,Int32 缓冲区大小,FileOptions 选项,SECURITY_ATTRIBUTES secAttrs,字符串 msgPath,布尔 bFromProxy)  
   在 System.IO.FileStream..ctor(字符串路径,FileMode 模式,FileAccess 访问)
   在 PTA.Forms.frmPIProject.SetCurrentPageProjects()###Soruce :###mscorlib### InnerException :
###### 数据:###System.Collections.ListDictionaryInternal
### 日志结束###

此错误发生在这行代码中

using (FileStream fs = new FileStream(@strfn, FileMode.Create, FileAccess.Write))
{
    fs.Write(bgImg, 0, bgImg.Length);
    fs.Flush();
    fs.Close();
}

非常感谢您对此的帮助

4

3 回答 3

3

也许您的目录没有被创建。最好不要吞下您的异常。从这段代码中删除 try / catch 并检查错误是否在这里:

if (!exists)
    Directory.CreateDirectory(folder);
else
{
    Directory.Delete(fullpath, true);
    Directory.CreateDirectory(folder);
}

我认为您的应用程序没有在 Program Files 文件夹中写入的正确权限。

于 2015-09-08T08:05:00.583 回答
0

在进行任何更改之前,请确保您已授予文件夹和文件的完全权限。

我通过关闭浏览器中的一项设置解决了这个问题。您可以在 IE 中禁用安全选项卡 > 自定义级别 > “将文件上传到服务器时包含本地目录路径”中的设置之一,然后重试。

当您将站点添加到受信任站点时,此设置将启用。从受信任的列表中删除您的站点或手动禁用此设置,问题将得到修复。

同样,如果您在其他浏览器上遇到相同的问题,请查找上述设置并将其标记为禁用,问题将得到解决。

于 2019-05-07T06:46:16.107 回答
0

从您的代码,您将其称为“临时”文件夹的事实,以及您甚至从不费心跟踪您创建的文件的名称这一事实,我有点怀疑您是否真的有任何理由将图像保存到磁盘。

您可以直接从字节中创建一个 Image 对象,并将其分配给您的控件:

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;
于 2015-09-08T09:47:47.287 回答