This is my code and my goal is to make the cloned images(Bitmap) opacity adjustable using the color matrix however, the tutorial on YouTube is insufficient for me hence, I need help on my project. Thank you very much in advance for helping me! If there are questions regarding on my project, just comment in and I will try to reply as soon as possible guys.
This is my code:
private FilterInfoCollection CaptureDevices;
private VideoCaptureDevice videoSource;
private void Form1_Load(object sender, EventArgs e)
{
{
CaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo Device in CaptureDevices)
{
comboBox1.Items.Add(Device.Name);
comboBox2.Items.Add(Device.Name);
}
comboBox1.SelectedIndex = 0;
videoSource = new VideoCaptureDevice();
comboBox2.SelectedIndex = 0;
videoSource = new VideoCaptureDevice();
}
}
private void s1_Click(object sender, EventArgs e)
{
videoSource = new VideoCaptureDevice(CaptureDevices[comboBox1.SelectedIndex].MonikerString);
videoSource.NewFrame += new NewFrameEventHandler(VideoSource_NewFrame);
videoSource.Start();
}
private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
}
private void s2_Click(object sender, EventArgs e)
{
videoSource = new VideoCaptureDevice(CaptureDevices[comboBox2.SelectedIndex].MonikerString);
videoSource.NewFrame += new NewFrameEventHandler(VideoSource_NewFrames);
videoSource.Start();
}
private void VideoSource_NewFrames(object sender, NewFrameEventArgs eventArgs)
{
pictureBox2.Image = (Bitmap)eventArgs.Frame.Clone();
}
private void r1_Click(object sender, EventArgs e)
{
videoSource.Stop();
pictureBox1.Image = null;
pictureBox1.Invalidate();
pictureBox3.Image = null;
pictureBox3.Invalidate();
}
private void r2_Click(object sender, EventArgs e)
{
videoSource.Stop();
pictureBox2.Image = null;
pictureBox2.Invalidate();
pictureBox4.Image = null;
pictureBox4.Invalidate();
}
private void c1_Click(object sender, EventArgs e)
{
pictureBox3.Image = (Bitmap)pictureBox1.Image.Clone();
}
private void c2_Click(object sender, EventArgs e)
{
pictureBox4.Image = (Bitmap)pictureBox2.Image.Clone();
}
Related
So, i try to learn how to Drag and Drop an Image from one PictureBox to another and that works well. But how can i drag and drop the Image TAG from pictureox1 to picturebox2?
i currently have 3 source images and 3 drop boxes.
the dropbox6 is locked with a countdown after a buttonclick (see button2)
(later i will lock all dropboxes)
after i hit this button a countdown starts and if the countdown is 0 (ZERO) only then i can drag one of the 3 images to this box.
however, i have given each of these 3 images in the souceboxes a TAG name.
how can i drop this tagname also to the dropbox?
here is what i have so far:
(the Label labeled "Counter" is actually Label4 in the 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;
namespace Game
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
// pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);
((PictureBox)sender).DoDragDrop(((PictureBox)sender).Image, DragDropEffects.Copy);
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox4.AllowDrop = true;
pictureBox5.AllowDrop = true;
pictureBox6.AllowDrop = true;
pictureBox6.Enabled = false;
}
private void pictureBox4_DragEnter(object sender, DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
}
}
private void pictureBox4_DragLeave(object sender, EventArgs e)
{
}
private void pictureBox4_DragDrop(object sender, DragEventArgs e)
{
Image getPicture = (Bitmap) e.Data.GetData(DataFormats.Bitmap);
pictureBox4.Image = getPicture;
}
private void pictureBox5_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
}
}
private void pictureBox5_DragLeave(object sender, EventArgs e)
{
}
private void pictureBox5_DragDrop(object sender, DragEventArgs e)
{
Image getPicture = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
pictureBox5.Image = getPicture;
}
private void pictureBox6_DragDrop(object sender, DragEventArgs e)
{
Image getPicture = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
pictureBox6.Image = getPicture;
timer1.Enabled = false;
}
private void pictureBox6_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
timer1.Enabled = false;
}
}
private void pictureBox6_DragLeave(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void pictureBox4_Click(object sender, EventArgs e)
{
MessageBox.Show("test");
}
private void timer1_Tick(object sender, EventArgs e)
{
counter--;
if (counter == 0)
timer1.Stop();
label4.Text = counter.ToString();
if(counter == 0)
{
pictureBox6.Enabled = true;
label4.Enabled = false;
timer1.Stop();
}
}
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
private void pictureBox6_Click(object sender, EventArgs e)
{
MessageBox.Show(pictureBox6.Tag.ToString());
}
private int counter = 3;
private void button2_Click(object sender, EventArgs e)
{
int counter = 3;
timer1 = new Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000; // 1 second
timer1.Start();
label4.Text = counter.ToString();
if(label4.Text == "0")
{
timer1.Stop();
}
pictureBox6.Image = Properties.Resources.shoreSite_d_1_l_x_x;
button2.Visible=false;
}
}
internal class bild1
{
private Bitmap shoreSite_d_1_l_x_x;
public bild1(Bitmap shoreSite_d_1_l_x_x)
{
this.shoreSite_d_1_l_x_x = shoreSite_d_1_l_x_x;
}
}
}
In MouseDown you are saying what you want to transmit in the DoDragDrop call. In your current case you are transmitting the image ((PictureBox)sender).Image but you can chose to transmit the tag as well...
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
((PictureBox)sender).DoDragDrop(((PictureBox)sender).Image, DragDropEffects.Copy);
((PictureBox)sender).DoDragDrop(((PictureBox)sender).Tag, DragDropEffects.Copy);
}
...Then make sure to parse for each possible input type in DragDrop
private void pictureBox6_DragDrop(object sender, DragEventArgs e)
{
var image = e.Data.GetData(DataFormats.Bitmap) as Bitmap;
var tag = e.Data.GetData(DataFormats.Text) as string;
if (image != null)
{
pictureBox4.Image = image;
}
if (tag != null)
{
pictureBox4.Tag = tag;
}
timer1.Enabled = false;
}
Beware, that you will need to update all the code that checks that the data is Bitmap before it arrives at DragDrop ie in DragEnter and write code that handles both Bitmap and Text, otherwise the Drag functionality will break.
AWESOME!!
It works now. I will continue this Project.
private void timer1_Tick(object sender, EventArgs e)
{
foreach (string image in filesRadar)
{
pictureBox1.Load(image);
}
}
private void timer2_Tick(object sender, EventArgs e)
{
foreach (string image in filesSatellite)
{
pictureBox2.Load(image);
}
}
I'm not getting any errors/exceptions and using breakpoint it stop on the Load line/s but it's not showing the images on any of the pictureBoxes.
This is working. could be in other ways but it's working.
int radImagesCount = 0;
private void timer1_Tick(object sender, EventArgs e)
{
radImagesCount++;
if (radImagesCount == filesRadar.Length)
{
radImagesCount = 0;
}
pictureBox1.Image = new Bitmap(filesRadar[radImagesCount]);
}
int satImagesCount = 0;
private void timer2_Tick(object sender, EventArgs e)
{
satImagesCount++;
if (satImagesCount == filesSatellite.Length)
{
satImagesCount = 0;
}
pictureBox2.Image = new Bitmap(filesSatellite[satImagesCount]);
}
Both filesRadar and filesSatellite are string[] arrays contains the images fils.
When ran the code which is GUI based and it actually opens a webcamera feed and saves frame every 5s, after a random running time(it could be 5 min or 20 min) there is an exception occuring ''Object in use by another process'' possibly related to the saving of the current frame. Any ideas what causes the problem and what code modifications should be done?
public partial class Form1 : Form
{
private FilterInfoCollection CaptureDevices;
private VideoCaptureDevice videoSource;
bool blncapturing;
public Form1()
{
InitializeComponent();
timer1.Enabled = false;
timer1.Interval = 5000;
blncapturing = false;
}
private void button1_Click(object sender, EventArgs e)
{
videoSource = new VideoCaptureDevice(CaptureDevices[comboBox1.SelectedIndex].MonikerString);
videoSource.NewFrame += new NewFrameEventHandler(VideoSource_NewFrame);
videoSource.Start();
timer1.Enabled = true;
}
private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
if (pictureBox1.Image != null)
((IDisposable)pictureBox1.Image).Dispose();
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
}
private void Form1_Load_1(object sender, EventArgs e)
{
CaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo Device in CaptureDevices)
{
comboBox1.Items.Add(Device.Name);
}
comboBox1.SelectedIndex = 0;
videoSource = new VideoCaptureDevice();
}
private void button2_Click(object sender, EventArgs e)
{
videoSource.Stop();
pictureBox1.Image = null;
pictureBox1.Invalidate();
pictureBox2.Image = null;
pictureBox2.Invalidate();
}
private void button3_Click(object sender, EventArgs e)
{
Capturing();
}
private void button4_Click(object sender, EventArgs e)
{
if (videoSource.IsRunning == true)
{
videoSource.Stop();
}
Application.Exit(null);
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
Capturing();
timer1.Start();
}
private void Capturing()
{
try
{
if (blncapturing == false)
{
blncapturing = true;
pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone();
string strGrabFileName = String.Format("C:\\Users\\echristo\\Desktop\\trial\\Snapshot_{0:yyyyMMdd_hhmmss.fff}.bmp", DateTime.Now);
pictureBox2.Image.Save(strGrabFileName, System.Drawing.Imaging.ImageFormat.Jpeg) ;
blncapturing = false;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
If you access objects from different threads you need a lock.
Also use invoke when you access a control from a different thread.
private readonly object myLock = new object();
private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
lock (myLock) {
this.Invoke((Action)(() => {
if (pictureBox1.Image != null)
((IDisposable)pictureBox1.Image).Dispose();
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
}));
}
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
// you can try with timer1 inside the lock
lock (myLock) {
// you don't need invoke here, because the winforms timer runs on the main thread.
Capturing();
}
timer1.Start();
}
Also as #TheGeneral said, dispose pictureBox2.Image after saving.
I have RadioButton which has a background. Now I need to change background on MouseEnter event. I can do that with
private void button_MouseEnter(object sender, EventArgs e)
{
button.BackgroundImage = Image.FromFile("D:/img/sample.png");
}
But I already have that image as resource in project and I don't know how to get to it.
try as follow:-
private void button_MouseEnter(object sender, EventArgs e)
{
button.BackgroundImage = <YourNameSpace>.Properties.Resources.<ResourceName>;
}
In case you use winforms, include the images to your resource:
public Form1()
{
InitializeComponent();
button1.MouseEnter += new EventHandler(button1_MouseEnter);
button1.MouseLeave += new EventHandler(button1_MouseLeave);
}
void button1_MouseLeave(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img1));
}
void button1_MouseEnter(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img2));
}
I have a simple bitmap editor for small monochrome bitmaps. And I have a menus which allows to choose thickness of the pen and robber. The problem is that I don't like the following code. Is there any way to make it more compact?
private void toolStripMenuItem4_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 3;
}
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 2;
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 1;
}
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 4;
}
private void toolStripMenuItem6_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 5;
}
private void toolStripMenuItem7_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 6;
}
private void toolStripMenuItem8_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 7;
}
private void toolStripMenuItem9_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 8;
}
private void toolStripButton3_Click_1(object sender, EventArgs e)
{
CurrentPanel.EditMode = EditMode.Clear;
}
private void toolStripDropDownButton2_Click(object sender, EventArgs e)
{
CurrentPanel.EditMode = EditMode.FreeHand;
}
private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
{
CurrentPanel.EditMode = EditMode.Clear;
}
private void toolStripMenuItem10_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 1;
}
private void toolStripMenuItem11_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 2;
}
private void toolStripMenuItem12_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 3;
}
private void toolStripMenuItem13_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 4;
}
private void toolStripMenuItem14_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 5;
}
private void toolStripMenuItem15_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 6;
}
private void toolStripMenuItem16_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 7;
}
private void toolStripMenuItem17_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 8;
}
private void toolStripMenuItem18_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 16;
}
private void toolStripMenuItem19_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 32;
}
private void toolStripMenuItem21_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 32;
}
private void toolStripMenuItem20_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 16;
}
private void toolStripMenuItemCommon_Click(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem) sender;
int thickness = (int)item.Tag;
CurrentPanel.PenThickness = thickness ;
}
Of course you need to initialize Tag of each ToolStripMenuItem and set toolStripMenuItemCommon_Click as a handler for Click event. You can do it in a for cycle for example:
for(int i = 1; i < 8; i++)
{
ToolStripMenuItem item = new ToolStripMenuItem();
item.Text = "Set thickness: " + i;
item.Click += toolStripMenuItemCommon_Click;
item.Tag = i;
// add item to strip container
}
Yes, there is:
private void SetPenThickness(int thickness)
{
CurrentPanel.PenThickness = thickness;
}
toolStripMenuItem1.Click += (s,e) => SetPenThickness(1);
toolStripMenuItem2.Click += (s,e) => SetPenThickness(2);
toolStripMenuItem3.Click += (s,e) => SetPenThickness(3);
toolStripMenuItem4.Click += (s,e) => SetPenThickness(4);
// ...
Try this:
private void toolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem toolStripMenuItem = (ToolStripMenuItem) sender;
switch (toolStripMenuItem.ID)
{
case "toolStripMenuItem4":
{
CurrentPanel.PenThickness = 3;
break;
}
..........
..........
}
}