更新已在使用的文件

时间:2014-08-22 10:37:07

标签: c# image filestream

在这里看到几个例子,但它们似乎与我的senorio不匹配。

我基本上使用

将图像设置为背景图像
if (ProductDetails.ProductBackPicFilePath != "")
{
mProdButList[i].BackgroundImage =  Image.FromFile(ProductDetails.ProductBackPicFilePath);
mProdButList[i].BackgroundImageLayout = ImageLayout.Stretch;
}  

在管理部分的程序的一部分中,我有其他代码从数据库中读取二进制数据并创建要加载的图像文件。

internal bool CreateImage(string pStockCode)
    {
        FileStream fs;                         
        BinaryWriter bw;                        
        int mBufferSize = 100;                   
        byte[] mOutbyte = new byte[mBufferSize];  
        long mRetval;                            
        long mStartIndex = 0;                                

        string getImgSQL =
            "SELECT STOCK_CODE, STOCK_PICTURE " +
            "FROM STOCK_DETAILS " +
            "WHERE STOCK_CODE = '"+pStockCode+"'";

        using (FbConnection DBConn = new FbConnection(cs.ToString()))
        {
            using (FbCommand fbCmd = new FbCommand(getImgSQL, DBConn))
            {
                DBConn.Open();
                FbDataReader myReader = fbCmd.ExecuteReader(CommandBehavior.SequentialAccess);

                while (myReader.Read())
                {  
// ERROR HERE WHEN CREATING A NEW FILESTREAM
                    fs = new FileStream("Product Images\\product_" + pStockCode + ".jpg", FileMode.OpenOrCreate, FileAccess.Write); 
                    bw = new BinaryWriter(fs);

                    mStartIndex = 0;

                    mRetval = myReader.GetBytes(1, mStartIndex, mOutbyte, 0, mBufferSize);

                    while (mRetval == mBufferSize)
                    {
                        bw.Write(mOutbyte);
                        bw.Flush();

                        mStartIndex += mBufferSize;
                        mRetval = myReader.GetBytes(1, mStartIndex, mOutbyte, 0, mBufferSize);
                    }

                    bw.Write(mOutbyte, 0, (int)mRetval);
                    bw.Flush();

                    bw.Close();
                    fs.Close();
                }

                myReader.Close();
                DBConn.Close();
            }
        }


        return true;
    }

我收到文件正在使用的错误,从技术上讲,它在另一种形式上,因为它仍然在后面层显示图片。

问题是如何强制它覆盖正在使用的图像?如果没有显示图像,这可以发现..

2 个答案:

答案 0 :(得分:1)

dot net依赖于Windows操作系统功能来加载和显示图像。窗口依次在显示时锁定图像源文件。即使图像从显示中删除,也可能需要一些时间来解锁文件。

一种方法是在图像未使用时显式调用Dispose()。这将释放源文件。

正如您所说,您希望在第一张图像仍然显示时显示另一张图像,您可以考虑在临时文件夹和不同的临时文件中生成图像。

另一种(有点奇怪的)方法是首先加载图像,但在将其作为背景应用之前,克隆它。克隆的映像将完全由内存支持,不会附加到文件。然后处理源图像。将克隆的图像设置为背景。

Image i1 = Image.FromFile(ProductDetails.ProductBackPicFilePath);
Image i2 = (Image)i1.Clone();
mProdButList[i].BackgroundImage = i2;
i1.Dispose();

答案 1 :(得分:0)

解决方案:

if (ProductDetails.ProductBackPicFilePath != "")
{
     byte[] fileContents = System.IO.File.ReadAllBytes(ProductDetails.ProductBackPicFilePath);

     using (System.IO.MemoryStream ms = new System.IO.MemoryStream(fileContents))
     {
          mProdButList[i].BackgroundImage = Image.FromStream(ms);                               
     }
     mProdButList[i].BackgroundImageLayout = ImageLayout.Stretch;
}

这可以帮助我们在数小时后尝试解决这个问题!

相关问题