C# 防止图像被阻止覆盖,因为它是作为背景动态加载的
编辑:也可以查看我以前关于这个程序的帖子来获取背景信息 我创建了一个图像查看器,它将所选图像显示为“窗体”的背景 在我关闭程序之前,我加载的所有图像都会被其他应用程序(如Photoshop或GIMP等图像处理应用程序)覆盖 我的代码非常简单:C# 防止图像被阻止覆盖,因为它是作为背景动态加载的,c#,image,background,overwrite,blocked,C#,Image,Background,Overwrite,Blocked,编辑:也可以查看我以前关于这个程序的帖子来获取背景信息 我创建了一个图像查看器,它将所选图像显示为“窗体”的背景 在我关闭程序之前,我加载的所有图像都会被其他应用程序(如Photoshop或GIMP等图像处理应用程序)覆盖 我的代码非常简单: public partial class Form1:Form { private string FileName; public Form1() { InitializeComponent(); Fil
public partial class Form1:Form {
private string FileName;
public Form1() {
InitializeComponent();
FileName = "";
openFileDialog1.Filter = "PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg";
}
private void button1_Click( object sender, EventArgs e ) {
if(openFileDialog1.ShowDialog() == DialogResult.OK) {
button2.Enabled = true;
FileName = openFileDialog1.FileName;
setImage();
}
}
private void button2_Click( object sender, EventArgs e ) {
setImage();
}
private void setImage() {
Stream str=new FileStream( FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
Bitmap tempImg= new Bitmap( Bitmap.FromStream(str) );
str.Close();
using( tempImg = new Bitmap(FileName) ) {
Rectangle rect = new Rectangle(0, 0, tempImg.Width, tempImg.Height);
PixelFormat format = tempImg.PixelFormat;
this.BackgroundImage = new Bitmap(FileName).Clone(rect, format);
}
openFileDialog1.FileName = "";
}
}
我怎样才能解决这个问题
更新:
这个代码对我不起作用
private void setImage() {
// using (FileStream stream = new FileStream("MyImage.png", FileMode.Open, FileAccess.Read))
//var image = Image.FromStream(stream);
using( new FileStream( FileName, FileMode.Open, FileAccess.Read, FileShare.Read) ) {
Stream str=new FileStream( FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
Bitmap tempImg= new Bitmap( Bitmap.FromStream(str) );
str.Close();
tempImg = new Bitmap(FileName);
Rectangle rect = new Rectangle(0, 0, tempImg.Width, tempImg.Height);
PixelFormat format = tempImg.PixelFormat;
this.BackgroundImage = new Bitmap(FileName).Clone(rect, format);
}
openFileDialog1.FileName = "";
}
在你的
setImage
中,你得到了str.Close()
两次…我的错,我刚刚在发布时添加了它^^除此之外,您还创建了3次位图
!!!你这样做是为了什么?这段代码是我在上一篇文章中向我建议的。post->我想你应该遵循这个答案。它能满足你的需要。把那个答案和你们在这个问题中发布的代码进行比较,我刚刚试过,运行了那个代码,在使用{}之后打开了paint.net,我能够编辑这个图像。
using (FileStream stream = new FileStream("MyImage.png", FileMode.Open, FileAccess.Read))
{
var image = Image.FromStream(stream);
// Do something with the image.
}
// The image will not be locked here.