vb.net 清除图片框中的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1403630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
clearing the image in a picturebox
提问by user85511
I have a form with a picturebox that will allow to draw a free hand picture.
我有一个带有图片框的表单,可以绘制自由手绘图。
I added the initialization of the image in the form_load
and the click event of the clear button. When I click the clear button the image is cleared and at the mouse move on the picturebox the last drawned image will shown.
我form_load
在清除按钮的点击事件中添加了图像的初始化。当我单击清除按钮时,图像被清除,鼠标在图片框上移动时,将显示最后绘制的图像。
My question is, how can I validate the picturebox whether it is empty or not? Just that I don't want to permit to save an empty image.
我的问题是,如何验证图片框是否为空?只是我不想允许保存空图像。
回答by adatapost
PictureBox1.Image = Nothing
回答by Crouchie
Private Sub ClearPictureBox(pb As PictureBox)
pb.Image = Nothing
pb.BackColor = Color.Empty
pb.Invalidate()
End Sub
回答by ali
if you need to reset your image in picturebox you can do this code (in c#)
如果您需要在图片框中重置图像,您可以执行此代码(在 c# 中)
private void your_name_picturebox_MouseDoubleClick(object sender, MouseEventArgs e)
{
your_name_picturebox.Image=null;
}
回答by Peter
C# bool clearImage; clearButton_Click(...) { clearImage = true; img.Invalidate(); }
C# bool clearImage; clearButton_Click(...) { clearImage = true; img.Invalidate(); }
img_Paint(...)
{
if (clearImage)
{
clearImage = false;
e.Graphics.Clear(Color.White);
}
}
VB.NET
网络
Private clearImage As Boolean
Private Sub button_click(ByVal sender As Object, ByVal e As eventargs) Handels....
clearImage = True
img.Invalidate()
End Sub
Private Sub img_Paint(ByVal sender As Object, ByVal e As ...) Handels ....
If clearImage Then
clearImage = False
e.Graphics.Clear(Color.White)
End If
End Sub
回答by xpda
There might be some logic error in your code. Check the mousemove or mousehover event and make sure it is not assigning an image to the picturebox.
您的代码中可能存在一些逻辑错误。检查 mousemove 或 mousehover 事件并确保它没有将图像分配给图片框。
Instead of assigning nothing to the new image with the button, you could clear the the existing image. This might be better because you won't have to create a new image before you can draw on it again.
您可以清除现有图像,而不是使用按钮为新图像分配任何内容。这可能会更好,因为您不必在重新绘制之前创建新图像。
g = Graphics.FromImage(PictureBox1.Image)
g.Clear(PictureBox1.BackColor)
回答by flip
I just drew a filled in blank rectangle to cover the image in the beginning of the code
我只是画了一个填充的空白矩形来覆盖代码开头的图像
Dim graph As Graphics = pbChart.CreateGraphics
'to clear image
graph.FillRectangle(Brushes.Azure, 0, 0, 500, 500)