I have a picturebox where i can load a image into its .backgroundimage function and now i have a button that is suppose to clear it but doesn't. I have tried using this code:
secondcapturebox.Dispose();
But yet still the image is in the picturebox.
Please Help,
Thanks
You have to set it to null. Like this:
private void button1_Click(object sender, EventArgs e) {
if (pictureBox1.BackgroundImage != null) {
pictureBox1.BackgroundImage.Dispose();
pictureBox1.BackgroundImage = null;
}
}
Have you tried...
secondcapturebox.BackgroundImage = null;
pictureBox1.Image = null;
pictureBox1.Invalidate();
pictureBox1.BringToFront();
pictureBox1.BackgroundImage = null;
Related
I have a problem about System.Windows.Forms.PictureBox. I want to show a image on monitor and capture it on the camera. So I use Winform which include a picturebox. The picture box is:
PictureBox pb = new PictureBox();
pb.WaitOnLoad = true;
When I set a bitmap to PictureBox and capture the image from camera,
// Show bmp1
this.image.Image = bmp1;
this.image.Invalidate();
this.image.Refresh();
// Delay 1s
UseTimerToDelay1s();
// Show bmp2
this.image.Image = bmp2;
this.image.Invalidate();
this.image.Refresh();
// Capture
CaptureImageFromCamera();
It only capture the bmp1.
If I add a small delay like this,
this.image.Image = bmp2;
this.image.Invalidate();
this.image.Refresh();
UseTimerToDelay100ms();
CaptureImageFromCamera();
It capture bmp2. The Image set method seem to be a async method. Does any method to confirm the image is set? Thanks.
I'd use the first Paint event after assigning the new Image.
You can give it a try using a very large image url from this site.
The example uses google logo image url. Copy the following code and make sure you assign event handlers to the events:
bool newImageInstalled = true;
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.WaitOnLoad = true;
}
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
if (!newImageInstalled)
{
newImageInstalled = true;
BeginInvoke(new Action(() =>
{
//Capturing the new image
using (var bm = new Bitmap(pictureBox1.ClientSize.Width,
pictureBox1.ClientSize.Height))
{
pictureBox1.DrawToBitmap(bm, pictureBox1.ClientRectangle);
var tempFile = System.IO.Path.GetTempFileName() + ".png";
bm.Save(tempFile, System.Drawing.Imaging.ImageFormat.Png);
System.Diagnostics.Process.Start(tempFile);
}
}));
}
}
private void button1_Click(object sender, EventArgs e)
{
newImageInstalled = false;
pictureBox1.ImageLocation =
"https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png";
}
private void button2_Click(object sender, EventArgs e)
{
newImageInstalled = false;
pictureBox1.Image = null;
}
I want move an image in one picture box to another and vice versa on a button click, I'm pretty sure my code should work in theory, but it just won't execute for some reason. Any help would be much appreciated please!!
Here's my code:
int chicken_move = 0;
private void button_Chicken_Click(object sender, EventArgs e)
{
chicken_move++;
if (chicken_move > 1)
{
chicken_move = 0;
}
switch (chicken_move)
{
case 0:
pictureBox_Micro.Image = pictureBox_Uncooked.Image;
pictureBox_Uncooked.Image = null;
break;
case 1:
pictureBox_Uncooked.Image = pictureBox_Micro.Image;
pictureBox_Micro.Image = null;
break;
}
You need to use swap technique to change picture. Otherwise, you will lost it after first click.
public partial class Form1 : Form
{
private bool isFirstOne;
Image forSwap;
public Form1()
{
InitializeComponent();
string path1 = #"C:\Users\...\...\somePic.png";
pictureBox1.Image = Image.FromFile(path1);
forSwap = null;
isFirstOne = false;
}
private void button1_Click(object sender, EventArgs e)
{
switch (isFirstOne)
{
case true:
forSwap = pictureBox2.Image;
pictureBox1.Image = pictureBox2.Image;
pictureBox2.Image = null;
break;
case false:
forSwap = pictureBox1.Image;
pictureBox2.Image = pictureBox1.Image;
pictureBox1.Image = null;
break;
}
isFirstOne = !isFirstOne;
}
}
Try not setting the Image of one pictureBox to the Image of the other.
Try setting the Image to a reference to the actual image (file, etc.).
Or try using pictureBox.ImageLocation to set it.
Considering you set the chicken_move variable as global, there is nothing wrong
with the code. Try providing image file location:
pictureBox_Micro.ImageLocation = "UncookedImage.jpg";
pictureBox_Uncooked.ImageLocation = "MicroImage.jpg";
I'm trying to programmatically create a axWindowsMediaPlayer and show it:
private void button1_Click(object sender, EventArgs e)
{
AxWMPLib.AxWindowsMediaPlayer wmplayer = new AxWMPLib.AxWindowsMediaPlayer();
wmplayer.Size = new Size(200, 200);
wmplayer.enableContextMenu = false; //here it throws an exception
this.Controls.Add(wmplayer);
}
but it says {Property set of 'enableContextMenu' cannot be invoked at this time.}
why is that? why can I set Size but not enableContextMenu?
I found the solution:
It is important that you do changes and/or function calls after you added wmplayer to this.Controls.
I don't know why wmplayer.Size worked, but it is definitively the exception..
private void button1_Click(object sender, EventArgs e)
{
AxWMPLib.AxWindowsMediaPlayer wmplayer = new AxWMPLib.AxWindowsMediaPlayer();
this.Controls.Add(wmplayer);
wmplayer.Size = new Size(200, 200);
wmplayer.enableContextMenu = false; //here it throws an exception
}
works perfectly fine..
I'm trying to take a picture element from a website and display it inside a PictureBox.. The code doesn't return any errors, but nothing is displayed.
I'm using a WebBrowser class and trying to display the elements using an event that invokes once the webpage is done loading
void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
pictureBox1.ImageLocation = wb.Document.GetElementById("ctl00_mainContent_identityBar_emblemImg").InnerText; // does nothing
label1.Text = "Last Played: " + wb.Document.GetElementById("ctl00_mainContent_lastPlayedLabel").InnerText; // works fine
}
Here's an example of the webpage I'm trying to pull the image from: http://halo.bungie.net/Stats/Halo3/Default.aspx?player=SmitherdxA27
^It's the bird with the orange background on that example.
Pretty easy:
private void Form1_Load(System.Object sender, System.EventArgs e)
{
webBrowser1.Navigate("http://halo.bungie.net/Stats/Halo3/Default.aspx?player=SmitherdxA27");
}
private void WebBrowser1_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
if ((e.Url.ToString() == "http://halo.bungie.net/Stats/Halo3/Default.aspx?player=SmitherdxA27"))
{
HtmlElement elem = webBrowser1.Document.GetElementById("ctl00_mainContent_identityStrip_EmblemCtrl_imgEmblem");
string src = elem.GetAttribute("src");
this.pictureBox1.ImageLocation = src;
}
}
Good luck!
It returns nothing because an image tag doesnt contain inner text. You need to use the the element and generate a bitmap.
public Bitmap GetImage(string id)
{
HtmlElement e = webBrowser1.Document.GetElementById(id);
IHTMLImgElement img = (IHTMLImgElement)e.DomElement;
IHTMLElementRenderFixed render = (IHTMLElementRenderFixed)img;
Bitmap bmp = new Bitmap(e.OffsetRectangle.Width, e.OffsetRectangle.Height);
Graphics g = Graphics.FromImage(bmp);
IntPtr hdc = g.GetHdc();
render.DrawToDC(hdc);
g.ReleaseHdc(hdc);
return bmp;
}
I want to do an if stament on an image
if (SortName.Image == Properties.Resources.RadioEmpty)
{
SortName.Image = Properties.Resources.Radio;
}
else
{
SortName.Image = Properties.Resources.RadioEmpty;
}
but its on working any idea what I'm doing wrong ?
o.k additional information
1.
//SortName = A picture box
//Properties.Resources.RadioEmpty = Resources\RadioEmpty.png
//Properties.Resources.Radio = Resources\Radio.png
2.
Nope no errors
3.I wanted to use a custom image for a radio button. A have a picture box with the above code on click. RadioEmpty is the default so I check to see if the picturebox's image is the same as image form the Resources folder is so do code.
i advise you use tag for this issue see this code
private void Form1_Load(object sender, EventArgs e)
{
//in form load the radio is checked or unckecked
//here my radio is unchecked at load
pictureBox1.Image = WindowsFormsApplication5.Properties.Resources.Add;
pictureBox1.Tag = "UnChecked";
}
private void pictureBox1_Click(object sender, EventArgs e)
{
//after pictiurebox clicked change the image and tag too
if (pictureBox1.Tag.ToString() == "Checked")
{
pictureBox1.Image = WinFormsApplication.Properties.Resources.Add;
pictureBox1.Tag = "UnChecked";
}
else
{
pictureBox1.Image = WinFormsApplication.Properties.Resources.Delete;
pictureBox1.Tag = "Checked";
}
}
compare the names. Something like this (unverified)
if (SortName.Image.Name.Equals(Properties.Resources.RadioEmpty.Name))
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap bm1;
Bitmap bm2;
private void button1_Click(object sender, EventArgs e)
{
bm1 = new Bitmap(Properties.Resources.firegirl1);
bm2 = new Bitmap(Properties.Resources.Zemli2);
pictureBox1.Image = bm1;
pictureBox2.Image = bm2;
if (pictureBox1.Image==pictureBox2.Image)
{
MessageBox.Show("Some");
}
else
{
MessageBox.Show("Differ");
}
}
}