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;
}
Related
I'm currently trying to learn how to use the print functions in C#, and I have a problem when I'm trying to print out labels in my windows forms application.
What I want to do, is when I click button 1, I want to put the text from the labels (or draw an image of them) into a document, that can be printed.
I'm still new to programming, so this print function is very confusing for me.
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString(label1.Text, label2.Text, label3.Text, label4.Text, label5.Text, label6.Text, label7.Text, label8.Text, label9.Text);
}
private void button1_Click(object sender, EventArgs e)
{
this.printDocument1.PrintPage += new
System.Drawing.Printing.PrintPageEventHandler
(this.printDocument1_PrintPage);
}
private void PrintPage(object o, PrintPageEventArgs e)
{
System.Drawing.Image img = System.Drawing.Image.FromFile("C://gul.PNG");
Point loc = new Point(10, 10);
e.Graphics.DrawImage(img, loc);
}
What do I need to do different, to be able to do this?
Use the Form.DrawToBitmap() method.
For example a form like this:
When the print button is pressed:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var pd = new PrintDocument();
pd.PrintPage+=(s,ev)=>
{
var bmp = new Bitmap(Width, Height);
this.DrawToBitmap(bmp, new Rectangle(Point.Empty, this.Size));
ev.Graphics.DrawImageUnscaled(bmp, ev.MarginBounds.Location);
ev.HasMorePages=false;
};
var dlg = new PrintPreviewDialog();
dlg.Document=pd;
dlg.ShowDialog();
}
}
With the result:
I have use the given below code to display the image on canvas and now I want to delete the displayed image on the long press on that image. I try the contextmenu for that but it not works. please anybody tell me how i can do it or properly used contextmenu for it
private void Stickers1_SelectionChanged(object sender, SelectionChangedEventArgs e) {
var selecteditem = e.AddedItems[0] as StickersImageListModel;
Stickers1.Visibility = Visibility.Collapsed;
// taking image from a list StickersImageListModel of images and bind with imageitem varaible
Image imageitem = new Image();
BitmapImage image = new BitmapImage(new System.Uri(selecteditem.Imageurl, UriKind.Absolute));
imageitem.Source = image;
//Add the images on canvas
my_canvas.Children.Add(imageitem);
imageitem.AllowDrop = true;
// DRag and drop the images on canvas
imageitem.ManipulationMode = ManipulationModes.All;
imageitem.ManipulationDelta += Drag_ManipulationDelta;
CompositeTransform ct = new CompositeTransform();
imageitem.RenderTransform = ct;
my_canvas.Visibility = Visibility.Visible;
}
You can use Holding event for image. However you have to note that Holding event wont be fired for desktop apps instead you have to use RightTapped event.
If you are targetting only to mobile apps you can use Holding event
// imageitem.RightTapped += Imageitem_RightTapped;
imageitem.Holding += Imageitem_Holding;
imageitem.IsRightTapEnabled = true;
imageitem.IsHoldingEnabled = true;
private void Imageitem_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
mycanvas.Children.Remove(sender as Image);
}
private void Imageitem_Holding(object sender, HoldingRoutedEventArgs e)
{
mycanvas.Children.Remove(sender as Image);
}
I've got application page where is view from street camera. Photo form camera is refreshing about every 30 sec.
I created a button which is used to refresh photo.
I want to add progress indicator which will be show every time when photo is downloading.
I don't know how and where exactly I have to add code.
I tried many examples but fail.
Because I don't really understand how to turn it on and off.
public void downloading()
{
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(ImageOpenReadCompleted);
webClient.OpenReadAsync(new Uri("http://przeprawa.swi.pl/cgi-bin/kam.cgi?6&1399042906515&" + Guid.NewGuid()));
}
private void ImageOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(e.Result);
image1.Source = bmp;
}
}
public void Refresh(object sender, EventArgs e)
{
downloading();
}
Make this change to your code:
public void downloading()
{
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(ImageOpenReadCompleted);
webClient.OpenReadAsync(new Uri("http://przeprawa.swi.pl/cgi-bin/kam.cgi?6&1399042906515&" + Guid.NewGuid()));
var _progressIndicator = new ProgressIndicator
{
IsIndeterminate = true,
IsVisible = true,
Text = "Downloading...",
};
SystemTray.SetProgressIndicator(this, _progressIndicator);
}
private void ImageOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(e.Result);
image1.Source = bmp;
var _progressIndicator = new ProgressIndicator
{
IsVisible = false,
};
SystemTray.SetProgressIndicator(this, _progressIndicator);
}
}
public void Refresh(object sender, EventArgs e)
{
downloading();
}
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");
}
}
}