Skip to main content
 首页 » 编程设计

C#之在其他程序中编辑时实时更新 BackgroundImage

2024年02月20日12wayfarer

首先我制作了一个带有拖放功能的桌面 AIR 应用程序来查看图 block 图像,但由于一些问题,我尝试在 C# 中制作相同的应用程序。你认为什么是更好的选择? (我知道这个小程序的性能不是问题,但我的意思是两者的难度和复杂性)

我正在构建一个简单的 TileViewer,因此在 openFileDialog 中选择了一张图片并将其设置为窗体的平铺 BackgroundImage。

我的问题是:

  1. 我一直在使用计时器 ( interval = 500 ) 来重新加载图像,因此如果我在 photoshop 中编辑图像,TileViewer 将自动重新加载更新后的图像(因为我将其保存在 photoshop 中)。

    但问题是 photoshop 没有这样做的权限,因为图像是在其他程序(我的 TileViewer)中打开的

  2. 我删除了计时器并制作了一个刷新按钮。但它是同样的问题。

我想复制位图数据,但后来出现错误,提示我没有足够的内存用于 32px x 32px img。

我的代码:

    private string FileName; 
 
    public Form1() { 
        InitializeComponent(); 
 
        FileName = ""; 
    } 
 
    // OPEN FILE btn 
    private void button1_Click( object sender, EventArgs e ) { 
        openFileDialog1.Filter = "PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg"; 
        if(openFileDialog1.ShowDialog() == DialogResult.OK) { 
            button2.Enabled = true; 
            FileName = openFileDialog1.FileName; 
            setImage(); 
        } 
    } 
 
    // REFRESH btn 
    private void button2_Click( object sender, EventArgs e ) { 
        if( FileName != "" ) { 
            setImage(); 
        } 
    } 
 
    private void setImage() { 
        Bitmap tempImg = new Bitmap( FileName ); 
        Rectangle rect = new Rectangle(0, 0, 100, 100); 
        PixelFormat format = tempImg.PixelFormat; 
 
        this.BackgroundImage = new Bitmap( FileName ).Clone( rect, format ); 
    } 

伙计们,如果您有任何建议或解决方案,请告诉我。

请您参考如下方法:

更新 2:

另一个问题是行 Rectangle rect = new Rectangle(0, 0, 100, 100);

在构造函数中,您应该传递新图像的宽度和高度,而不是像 100 这样的任意值。

这就是为什么运行时说你内存不足!! (它对我有用,我有一台 18 GB 的巨型机器)

更新:

您正在泄漏对象,因此出现内存不足异常

(Haans 已经警告过你关于处理对象的问题。)

试试下面的代码

private string FileName; 
 
public Form1() 
{ 
    InitializeComponent(); 
 
    FileName = ""; 
} 
 
// OPEN FILE btn  
private void button1_Click(object sender, EventArgs e) 
{ 
    openFileDialog1.Filter = "PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg"; 
    if (openFileDialog1.ShowDialog() == DialogResult.OK) 
    { 
 
        FileName = openFileDialog1.FileName; 
        setImage(); 
    } 
} 
 
// REFRESH btn  
private void button2_Click(object sender, EventArgs e) 
{ 
    if (FileName != "") 
    { 
        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) 
        { 
 
 
        Rectangle rect = new Rectangle(0, 0, tempImg.Width, tempImg.Height); 
 
        PixelFormat format = tempImg.PixelFormat; 
 
        this.BackgroundImage = new Bitmap(tempImg); 
 
        } 
} 

旧答案

就像杰森说的

你可能需要在你的 setimage 方法中做这样的事情

Stream str = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read); 
   this.BackgroundImage  = Bitmap.FromStream(str); 
str.Close(); 

请注意,您最好在使用后关闭任何打开的流。

代替

私有(private)无效 setImage() {
位图 tempImg = new Bitmap( FileName ); <--- 矩形 rect = new 矩形(0, 0, 100, 100);
PixelFormat 格式 = tempImg.PixelFormat;

    this.BackgroundImage = new Bitmap( FileName ).Clone( rect, format );   
}