图片框上的清晰图像

发布于 2024-11-04 20:39:49 字数 908 浏览 0 评论 0原文

如何清除picturebox上的绘制图像? 以下内容对我没有帮助:

pictbox.Image = null;
pictbox.Invalidate();

请帮忙。

编辑

private void pictbox_Paint(object sender, PaintEventArgs e) 
{ 
     Graphics g = e.Graphics; 
     vl.Draw(g, ref tran.realListForInsert); 
} 

public void Draw(Graphics g, ref List<double> arr) 
{ 
    g.DrawRectangle(new Pen(Brushes.Red, 3), nodeArr[Convert.ToInt32(templstName)].pict.Location.X, nodeArr[Convert.ToInt32(templstName)].pict.Location.Y, 25, 25); 
    g.DrawRectangle(new Pen(Brushes.Green, 3), nodeArr[Convert.ToInt32(templstArgName)].pict.Location.X, nodeArr[Convert.ToInt32(templstArgName)].pict.Location.Y, 25, 25); 
    nodeArr[Convert.ToInt32(templstName)].text.Text = arr[Convert.ToInt32(templstArgName)].ToString(); 
    arr[Convert.ToInt32(templstName)] = arr[Convert.ToInt32(templstArgName)]; 
} 

How can I clear draw image on picturebox?
The following doesn't help me:

pictbox.Image = null;
pictbox.Invalidate();

Please help.

EDIT

private void pictbox_Paint(object sender, PaintEventArgs e) 
{ 
     Graphics g = e.Graphics; 
     vl.Draw(g, ref tran.realListForInsert); 
} 

public void Draw(Graphics g, ref List<double> arr) 
{ 
    g.DrawRectangle(new Pen(Brushes.Red, 3), nodeArr[Convert.ToInt32(templstName)].pict.Location.X, nodeArr[Convert.ToInt32(templstName)].pict.Location.Y, 25, 25); 
    g.DrawRectangle(new Pen(Brushes.Green, 3), nodeArr[Convert.ToInt32(templstArgName)].pict.Location.X, nodeArr[Convert.ToInt32(templstArgName)].pict.Location.Y, 25, 25); 
    nodeArr[Convert.ToInt32(templstName)].text.Text = arr[Convert.ToInt32(templstArgName)].ToString(); 
    arr[Convert.ToInt32(templstName)] = arr[Convert.ToInt32(templstArgName)]; 
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

月下凄凉 2024-11-11 20:39:49

设置 Image 属性 为 null 就可以了。它将清除图片框中当前显示的任何图像。确保您编写的代码与如下所示完全相同:

picBox.Image = null;

Setting the Image property to null will work just fine. It will clear whatever image is currently displayed in the picture box. Make sure that you've written the code exactly like this:

picBox.Image = null;
森罗 2024-11-11 20:39:49

正如其他人所说,将 Image 属性设置为 null 应该可以。

如果没有,则可能意味着您使用了 InitialImage 属性来显示您的图像。如果确实如此,请尝试将该属性设置为 null

pictBox.InitialImage = null;

As others have said, setting the Image property to null should work.

If it doesn't, it might mean that you used the InitialImage property to display your image. If that's indeed the case, try setting that property to null instead:

pictBox.InitialImage = null;
最美的太阳 2024-11-11 20:39:49
if (pictureBox1.Image != null)
{
    pictureBox1.Image.Dispose();
    pictureBox1.Image = null;
}
if (pictureBox1.Image != null)
{
    pictureBox1.Image.Dispose();
    pictureBox1.Image = null;
}
娇俏 2024-11-11 20:39:49

您需要以下内容:

pictbox.Image = null;
pictbox.update();

You need the following:

pictbox.Image = null;
pictbox.update();
我的影子我的梦 2024-11-11 20:39:49
private void ClearBtn_Click(object sender, EventArgs e)
{
    Studentpicture.Image = null;
}
private void ClearBtn_Click(object sender, EventArgs e)
{
    Studentpicture.Image = null;
}
缱绻入梦 2024-11-11 20:39:49

我假设您想清除通过 PictureBox 绘制的图像。

这可以通过 Bitmap 对象并使用 Graphics 对象来实现。你可能正在做类似的事情,

Graphics graphic = Graphics.FromImage(pictbox.Image);
graphic.Clear(Color.Red) //Color to fill the background and reset the box

这就是你正在寻找的吗?

编辑

由于您使用的是绘制方法,这会导致每次都重新绘制,我建议您在表单级别设置一个标志,指示是否应该绘制 Picturebox

private bool _shouldDraw = true;
public bool ShouldDraw
{
    get { return _shouldDraw; }
    set { _shouldDraw = value; }
}

在您的绘制中只需使用

if(ShouldDraw)
  //do your stuff

当您单击按钮时,将此属性设置为 false 应该没问题。

I assume you want to clear the Images drawn via PictureBox.

This you would be achieved via a Bitmap object and using Graphics object. you might be doing something like

Graphics graphic = Graphics.FromImage(pictbox.Image);
graphic.Clear(Color.Red) //Color to fill the background and reset the box

Is this what you were looking out?

EDIT

Since you are using the paint method this would cause it to be redrawn every time, I would suggest you to set a flag at the formlevel indicating whether it should or not paint the Picturebox

private bool _shouldDraw = true;
public bool ShouldDraw
{
    get { return _shouldDraw; }
    set { _shouldDraw = value; }
}

In your paint just use

if(ShouldDraw)
  //do your stuff

When you click the button set this property to false and you should be fine.

前事休说 2024-11-11 20:39:49

为了便于理解:

根据您实现目标的方式,请记住开发人员有责任处置不再使用或不再需要的所有内容。

这意味着:您随 pictureBox 创建的所有内容(即:图形、列表等)都将在不再需要时被丢弃。

例如:
假设您有一个图像文件加载到 PictureBox 中,并且您希望以某种方式删除该文件。
如果您没有正确从 PictureBox 中卸载图像文件;您将无法删除该文件,因为这可能会引发异常,表明该文件正在使用。

因此,您需要执行以下操作:

pic_PhotoDisplay.Image.Dispose();
pic_PhotoDisplay.Image = null;
pic_PhotoDisplay.ImageLocation = null;
// Required if you've drawn something in the PictureBox. Just Don't forget to Dispose Graphic.
pic_PhotoDisplay.Update();

// Depending on your approach; Dispose the Graphics with Something Like:
gfx = null;
gfx.Clear();
gfx.Dispose();

希望这对您有所帮助。

For the Sake of Understanding:

Depending on how you're approaching your objective(s), keep in mind that the developer is responsible to Dispose everything that is no longer being used or necessary.

This means: Everything you've created along with your pictureBox (i.e: Graphics, List; etc) shall be disposed whenever it is no longer necessary.

For Instance:
Let's say you have a Image File Loaded into your PictureBox, and you wish to somehow Delete that file.
If you don't unload the Image File from PictureBox correctly; you won't be able to delete the file, as this will likely throw an Exception saying that the file is being used.

Therefore you'd be required to do something like:

pic_PhotoDisplay.Image.Dispose();
pic_PhotoDisplay.Image = null;
pic_PhotoDisplay.ImageLocation = null;
// Required if you've drawn something in the PictureBox. Just Don't forget to Dispose Graphic.
pic_PhotoDisplay.Update();

// Depending on your approach; Dispose the Graphics with Something Like:
gfx = null;
gfx.Clear();
gfx.Dispose();

Hope this helps you out.

要走干脆点 2024-11-11 20:39:49

我也曾有过一个顽固的形象,它不会消失
通过将 Image 和 InitialImage 设置为 null。
要从图片框中永久删除图像,我必须使用下面的代码,
通过重复调用 Application.DoEvents() :

            Application.DoEvents();
            if (_pictureBox.Image != null)
                _pictureBox.Image.Dispose();
            _pictureBox.Image = null;
            Application.DoEvents();
            if (_pictureBox.InitialImage != null)
                _pictureBox.InitialImage.Dispose();
            _pictureBox.InitialImage = null;
            _pictureBox.Update();
            Application.DoEvents();
            _pictureBox.Refresh();

I've had a stubborn image too, that wouldn't go away
by setting the Image and InitialImage to null.
To remove the image from the pictureBox for good, I had to use the code below,
by calling Application.DoEvents() repeatedly:

            Application.DoEvents();
            if (_pictureBox.Image != null)
                _pictureBox.Image.Dispose();
            _pictureBox.Image = null;
            Application.DoEvents();
            if (_pictureBox.InitialImage != null)
                _pictureBox.InitialImage.Dispose();
            _pictureBox.InitialImage = null;
            _pictureBox.Update();
            Application.DoEvents();
            _pictureBox.Refresh();
娇俏 2024-11-11 20:39:49

我必须在 Image = null 之后添加 Refresh() 语句才能使事情正常工作。

I had to add a Refresh() statement after the Image = null to make things work.

迷荒 2024-11-11 20:39:49
pictureBox1.Image= new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image= new Bitmap(pictureBox1.Width, pictureBox1.Height);
檐上三寸雪 2024-11-11 20:39:49

就这么简单!
您可以使用按钮单击事件,
我将它与按钮属性名称一起使用:“btnClearImage”

// Note 1a:
// after clearing the picture box 
// you can also disable clear button 
// by inserting follwoing one line of code:

btnClearImage.Enabled = false   

// Note 1b:
// you should set your button Enabled property
// to "False"
// after that you will need to Insert 
// the following line to concerned event or button
// that load your image into picturebox1 
// code line is as follows:

btnClearImage.Enabled = true;

Its so simple!
You can go with your button click event,
I used it with a button property Name: "btnClearImage"

// Note 1a:
// after clearing the picture box 
// you can also disable clear button 
// by inserting follwoing one line of code:

btnClearImage.Enabled = false   

// Note 1b:
// you should set your button Enabled property
// to "False"
// after that you will need to Insert 
// the following line to concerned event or button
// that load your image into picturebox1 
// code line is as follows:

btnClearImage.Enabled = true;
萌能量女王 2024-11-11 20:39:49

我用这个方法来清除图片框中的图像。它可能会帮助某人

private void btnClear1_Click(object sender, EventArgs e)
        {

            img1.ImageLocation = null;
        }

I used this method to clear the image from picturebox. It may help some one

private void btnClear1_Click(object sender, EventArgs e)
        {

            img1.ImageLocation = null;
        }
挖个坑埋了你 2024-11-11 20:39:49

你应该尝试一下。当您清除图形时,您必须选择颜色。 SystemColors.Control 是表单的本机颜色

Graphics g = pB.CreateGraphics();
g.Clear(SystemColors.Control);

You should try. When you clear your Graphics you must choose color. SystemColors.Control is native color of form

Graphics g = pB.CreateGraphics();
g.Clear(SystemColors.Control);
策马西风 2024-11-11 20:39:49

在c# winform应用程序中清除pictureBox
在c# winform应用程序中清除pictureBox的简单方法

clear pictureBox in c# winform Application
Simple way to clear pictureBox in c# winform Application

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文