I'm currently trying to make a simple paint program in C# using a Windows Forms Application. When converting my list of Points to an Array using the ToArray function, I'm getting a generic "ArgumentException was unhandled: Parameter is invalid" error. I know I've done this before and it's worked fine, is there something special about the DrawLines function that I'm not aware of? Below is the code, with the line in question being the last line in the panel1_Paint event. Thanks in advance for any help you can provide.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GetSig
{
public partial class Form1 : Form
{
bool paint = false;
List<Point> myPointList = new List<Point>();
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
paint = true;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (paint)
{
myPointList.Add(e.Location);
panel1.Invalidate();
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
paint = false;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLines(Pens.Black, myPointList.ToArray());
}
}
}
According to the MSDN:
The first two points in the array specify the first line.
You need minimum two points in your Array.
Well, you can't draw lines without at least two points :)
if (myPointList.Count >= 2)
{
e.Graphics.DrawLines(Pens.Black, myPointList.ToArray());
}
Most likely that exception is not coming from the call to ToArray but from e.Graphics.DrawLines.
Related
I made this program for taking an Image from my Logitech HD Pro WebCam c920.
The program actually work for Integrated Camera, but when i want to take the flow of my Logitech Webcam
i have nothing... (No image, no errors).
I wanted to know if someone have the same problems or something to help me to fix it...
My program is only for taking an picture from my WebCam logitech on c# with aForge.
I have this code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Accord;
using AForge.Video;
using AForge.Video.DirectShow;
namespace WindowsFormsApp1
{
public partial class Webcam : Form
{
public Webcam()
{
InitializeComponent();
}
FilterInfoCollection filterInfoCollection;
VideoCaptureDevice videoCaptureDevice;
private void btnStart_Click(object sender, EventArgs e)
{
videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[cboCamera.SelectedIndex].MonikerString);
videoCaptureDevice.NewFrame += VideoCaptureDevice_NewFrame;
videoCaptureDevice.Start();
}
private void VideoCaptureDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
pic.Image = (Bitmap)eventArgs.Frame.Clone();
}
private void Webcam_Load(object sender, EventArgs e)
{
filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach(FilterInfo filterinfo in filterInfoCollection)
{
cboCamera.Items.Add(filterinfo.Name);
cboCamera.SelectedIndex = 0;
videoCaptureDevice = new VideoCaptureDevice();
}
}
private void Webcam_FormClosing(object sender, FormClosingEventArgs e)
{
if (videoCaptureDevice.IsRunning == true)
videoCaptureDevice.SignalToStop();
}
btnstart is button, cboCamera is a Combobox, and pic is a picturebox.
I had a very similar problem. While I do not know why this occurs I have tested the following solutions:
Using Media Capture API - Seem to work with all web cameras, but I had quite a bit of stability problems.
Using this Versatile WebCam C# library - Seem to work fine.
I have not tested Emgu/OpenCV, but that is another alternative.
Just recently began tinkering with Awesomium, it's very cool and much better than the stock webBrowser for WinForms.
However, when I use the _LoadingFrameComplete method to determine if the page has loaded, it seems to be firing 10+ times (when used on Facebook, 2 times when navigating to google.com)
I am trying to get the comparable method of webBrowser1_DocumentCompleted (which only fires one time, after the document has completed).
Is this a 'me' problem, or am I using the wrong methods to check whether the website has finished loading completely.
I'm using Visual C# 2010 Edition
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Debugging_Problems
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string searchURL = textBox1.Text;
webControl1.Source = new Uri(searchURL);
}
private void Awesomium_Windows_Forms_WebControl_LoadingFrameComplete(object sender, Awesomium.Core.FrameEventArgs e)
{
richTextBox1.AppendText("Completed.\n");
}
}
}
You need to use IsMainFrame
private void Awesomium_Windows_Forms_WebControl_LoadingFrameComplete(object sender, Awesomium.Core.FrameEventArgs e)
{
if (e.IsMainFrame)
{
richTextBox1.AppendText("Completed.\n");
}
}
Try putting if(e.IsMainFrame) { .... } inside your LoadingFrameComplete event handler and only put your code in there. – Jon
That was the answer. Thank you.
I am trying to figure out, for an assignment, a way to display a message that basically says "combo box is empty" after I remove all items from a combo box in C#. The assignment is very simple. I write a windows form in C# that is populated with fifteen states in a ComboBox and when I select an item from that list it is removed. I have it working but once all the items are gone It just sits there and I have to manually exit. Can someone point me in the right direction to get this to work, I am thinking maybe if statement is in order? Here is my code so far...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Chapter_15_Ex._15._3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Items.Remove(comboBox1.SelectedItem);
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Thank You
if (comboBox1.Items.Count == 0)
{
MessageBox.Show("Your combo is empty");
}
Check the number of items left, after you remove one. You can Count on the Items collection to see how many items are left in the ComboBox.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Items.Remove(comboBox1.SelectedItem);
if (comboBox1.Items.Count == 0)
MessageBox.Show("All Gone!");
}
You can check for the condition below
if (comboBox1.Items.Count == 0)
{
MessageBox.Show("Empty");
}
New to C# from vb.net and I am just making some mock bound applications for now. I have problems with the following code. If I pic an image and exit the application, there is no change. Even if I move a row. However if I upload an image, move to another row, then add another image. After exiting the application the first image will be there but not the second.
In short I have to attempt to upload to another record before the record I actually want updating will do so.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace DBUserManagement
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'dsUsers.Users' table. You can move, or remove it, as needed.
this.usersTableAdapter.Fill(this.dsUsers.Users);
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (DialogResult.OK == ofd.ShowDialog())
{
imgUser.SizeMode = PictureBoxSizeMode.StretchImage;
imgUser.Image = new Bitmap(ofd.OpenFile());
//update bound field.
usersTableAdapter.Update(dsUsers);
}
}
}
}
Any ideas on what I am missing or not understand correctly? Any help appreciated.
/P
The answer was I needed to call the BindingSource's .EndEdit(); method.
So I am guessing it was down to the binding source still having hold of something.
Seems like I'm on the right track anyhow, I looked up the details on MSDN :)
I am trying to make a tool that allows me to choose a certain location on a picturebox to put text from a textbox on. It will need to be able to place multiple different texts on the picturebox and then be able to be deleted. This is my current code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TextboxTool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Visible = true;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
Graphics G = Graphics.FromImage(pictureBox1.Image);
G.DrawString(textBox1.Text, new Font("Tahoma", 40), Brushes.Black, new Point(MousePosition.X, MousePosition.Y));
}
}
}
At the moment i can type the text in the textbox, but can't draw the string on the picturebox and choose its location. I have a button which is meant to confirm the text written is right and then allow the user to choose its location. Please can someone help me sort this code out?
Thanks-
The MousePosition property is relative to the screen, not the PictureBox.
You should handle the MouseClick event and draw the string at e.X and e.Y.
Alternatively, you can call pictureBox1.PointToClient to transform screen coordinates to control-relative coordinates.
Also, you should dispose the Graphics object in a using statement.
Finally, I'm pretty sure you'll need to call pictureBox1.Invalidate() after modifying the image to force it to repaint.