C# 中删除 PictureBox 中的图像

发布于 2024-10-09 21:36:09 字数 329 浏览 2 评论 0原文

当用户按“del”键时如何从图片框中删除图像...我没有找到 PB 的任何按键或按键事件。

    private void topRight_pbx_MouseClick(object sender, MouseEventArgs e)
          {
           imgSelected=true;

           //need to accept "delete"key from keyboard?

           topRight_pbx.Image = null;
            topRFile = "";

           }

how to delete image from picture box when user press "del" key...I dont find any keypress or keydown events for PB.

    private void topRight_pbx_MouseClick(object sender, MouseEventArgs e)
          {
           imgSelected=true;

           //need to accept "delete"key from keyboard?

           topRight_pbx.Image = null;
            topRFile = "";

           }

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

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

发布评论

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

评论(4

〃温暖了心ぐ 2024-10-16 21:36:09

将您的 imgSelected 更改为类似以下内容:

private PictureBox picSelected = null;

在图片框中单击将此变量设置为发送者:

picSelected = (PictureBox)sender;

然后在表单的按键或具有焦点的控件上运行图像删除代码
(表格示例):

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.Delete)
      picSelected.Image = null;
}

Change your imgSelected to something like:

private PictureBox picSelected = null;

On your picturebox click set this variable to the sender:

picSelected = (PictureBox)sender;

Then on the keydown of form or the control that has focus you run the image removal code
(Example for form):

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.Delete)
      picSelected.Image = null;
}
后来的我们 2024-10-16 21:36:09

PictureBox 控件永远无法获得焦点,并且非焦点控件不会接收键盘输入事件。

这是因为 microsoft.com/en-us/library/system.windows.forms.picturebox.keydown.aspx" rel="nofollow">文档 显示,KeyDown 事件(以及其他事件与键盘输入相关的)被标记为 [BrowsableAttribute(false)] 因为它们不能按预期工作。它们不打算由您的代码订阅。

它类似于 Label 控件 - 您可以查看它,但它不可选择且无法获取焦点。

您需要找到另一种方式让用户表明他想要删除当前显示在 PictureBox 控件中的图像。

That's because the PictureBox control can never get the focus, and non-focused controls do not receive keyboard input events.

As the documentation shows, the KeyDown event (and the other events related to keyboard input) are marked with [BrowsableAttribute(false)] because they do not work as expected. They are not intended to be subscribed to by your code.

It's similar to a Label control—you can look at it, but it's not selectable and can't acquire the focus.

You'll need to find another way for the user to indicate that (s)he wants to delete an image currently displayed in a PictureBox control.

难忘№最初的完美 2024-10-16 21:36:09

我在我的一个项目中遇到了类似的问题。我通过添加一个离屏文本框解决了这个问题。当单击某些控件时,我将焦点放在文本框上,并使用文本框来处理键盘输入。

PicureBox SelectedImage=null;

void Image_Click(object sender,...)
{
  SelectedImage=(PictureBox)sender;
  FocusProxy.Focus();
}

void FocusProxy_KeyDown(...)
{
  if(e.KeyData==...)
  {
       SelectedImage.Image=null;
       e.Handled=true;
  }
}

I had a similar problem in one of my projects. I solved it by adding an off-screen textbox. I give focus to the text-box when certain controls get clicked, and use the text-box to handle the keyboard input.

PicureBox SelectedImage=null;

void Image_Click(object sender,...)
{
  SelectedImage=(PictureBox)sender;
  FocusProxy.Focus();
}

void FocusProxy_KeyDown(...)
{
  if(e.KeyData==...)
  {
       SelectedImage.Image=null;
       e.Handled=true;
  }
}
幽梦紫曦~ 2024-10-16 21:36:09

另一种方法可能是:
如果您在 pictureBox 上绘图并且想要清除它:

Graphics g = Graphics.FromImage(this.pictureBox1.Image);
g.Clear(this.pictureBox1.BackColor);

之后您可以在控件上再次绘图。

我希望这可以帮助某人

A different way for this could be:
If you are drawing on a pictureBox and you want to clear it:

Graphics g = Graphics.FromImage(this.pictureBox1.Image);
g.Clear(this.pictureBox1.BackColor);

After that you can draw again on the control.

I hope this can help to someone

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