简体   繁体   English

使用FileStream或Simple Bitmap在c#中更新后未保存图像

[英]Image is not saving after updating, in c# using FileStream or Simple Bitmap

I am working for a utility in desktop application, related to some graphics work. 我正在为桌面应用程序中的某个实用程序工作,该实用程序与某些图形工作有关。

I have to load small thumbnails and full sized bitmap.I am doing this using this code. 我必须加载小的缩略图和完整的位图,我正在使用此代码进行此操作。

 FileStream fs = new FileStream("myphoto.jpg", FileMode.Open);
 Image imgPhoto = Image.FromStream(fs);
 fs.Close();
 PictureBox p = new PictureBox();
 p.Name = "p1";
 p.Image = imgPhoto;
 flowPanel.Controls.Add(p);
 //---- 
 FileStream fs = new FileStream("myphoto_thumb.jpg", FileMode.Open);
 Image img = Image.FromStream(fs);
 fs.Close();
 Button b_thumb = new Button();
 b_thumb.Name = "thumb1";
 b_thumb.BackgroundImage = img;
 flowBottom.Controls.Add(b_thumb);

After this, i need to add some effects on image of this picturebox.When i add some effect to main (big) image in picturebox, i also add the same effect in botton background image.When i save this thumbnail image to existing thumbnail image it is saving, but when i save the big image from picturebox on the existing file "myphoto.jpg", it gives me error like FILE IS BEING USED BY ANOTHER PROCESS 之后,我需要在此图片框的图像上添加一些效果。当我向图片框中的主(大)图像添加一些效果时,我也会在botton背景图像中添加相同的效果。当我将此缩略图保存到现有的缩略图中时它正在保存,但是当我将Picturebox中的大图像保存在现有文件“ myphoto.jpg”上时,它给了我错误,例如文件正在由其他过程使用

//---- Add some Effects to image of Picturebox ---- //
PictureBox tempPic = flowPanel.Controls["p1"];
tempPic.Image.Save("myphoto.jpg",ImageFormat.Jpeg);

I have found many solutions on google and on stackoverflow but could not find helpful. 我在Google和stackoverflow上找到了许多解决方案,但找不到帮助。 If any good solution, plz help. 如果有任何好的解决方案,请帮助。 May be its very simple but i have tried more than 10 hours but failed. 可能很简单,但我尝试了十多个小时,但失败了。

You don't close the FileStream you open. 您不会关闭打开的FileStream。 I always wrap this kind of stuff in a using construction, so it automatically gets destructed, thus closes: 我总是将这种东西包装在一个using构造中,因此它会自动被破坏,从而关闭:

using (FileStream fs = new FileStream("myphoto.jpg", FileMode.Open)) {
  // Do stuff
}

tempPic.Image.Save("myphoto.jpg",ImageFormat.Jpeg);

Try setting your filestream to read only: 尝试将文件流设置为只读:

fs = New System.IO.FileStream("myphoto.jpg", 
     IO.FileMode.Open, IO.FileAccess.Read)

This is Microsoft's recommendation, targeted at users who try Image.FromFile and it locks the file: http://support.microsoft.com/kb/309482 这是Microsoft的建议,针对尝试Image.FromFile并锁定文件的用户: http : //support.microsoft.com/kb/309482

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM