I'm trying to add images already used to an imageList named "Images1".
I'm not really good in coding programs but I know that the rest of my program is working except the fact that "Images1" does not exist for the two last voids. I searched to resolve this problem but I have difficulty to find an answer for this specific case.
How can I make my image list "Images1" available for all my private voids?
This is my 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 System.Collections;
using System.Globalization;
using System.Resources;
using System.Reflection;
namespace Booster_pack_2
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
List<Image> Images1 = new List<Image>();
ResourceManager rm = Booster_pack_2.Properties.Resources.ResourceManager;
string index1 = textBox1.Text;
Bitmap image1 = (Bitmap)rm.GetObject(index1);
pictureBox1.Image = image1;
Images1.Add((Image)Booster_pack_2.Properties.Resources.ResourceManager.GetObject(index1));
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
ResourceManager rm = Booster_pack_2.Properties.Resources.ResourceManager;
string index2 = textBox2.Text;
Bitmap image2 = (Bitmap)rm.GetObject(index2);
pictureBox2.Image = image2;
Images1.Add((Image)Booster_pack_2.Properties.Resources.ResourceManager.GetObject(index2));
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
ResourceManager rm = Booster_pack_2.Properties.Resources.ResourceManager;
string index3 = textBox3.Text;
Bitmap image3 = (Bitmap)rm.GetObject(index3);
pictureBox3.Image = image3;
Images1.Add((Image)Booster_pack_2.Properties.Resources.ResourceManager.GetObject(index3));
}
}
}
Put it in the proper scope. If you need to access it in all your event handlers, make it a class member:
namespace Booster_pack_2
{
public partial class Form3 : Form
{
List<Image> Images1;
public Form3()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
Images1 = new List<Image>();
ResourceManager rm = Booster_pack_2.Properties.Resources.ResourceManager;
string index1 = textBox1.Text;
Bitmap image1 = (Bitmap)rm.GetObject(index1);
pictureBox1.Image = image1;
Images1.Add((Image)Booster_pack_2.Properties.Resources.ResourceManager.GetObject(index1));
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
ResourceManager rm = Booster_pack_2.Properties.Resources.ResourceManager;
string index2 = textBox2.Text;
Bitmap image2 = (Bitmap)rm.GetObject(index2);
pictureBox2.Image = image2;
Images1.Add((Image)Booster_pack_2.Properties.Resources.ResourceManager.GetObject(index2));
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
ResourceManager rm = Booster_pack_2.Properties.Resources.ResourceManager;
string index3 = textBox3.Text;
Bitmap image3 = (Bitmap)rm.GetObject(index3);
pictureBox3.Image = image3;
Images1.Add((Image)Booster_pack_2.Properties.Resources.ResourceManager.GetObject(index3));
}
}
}
I'd advise moving this line: Images1 = new List();
to the constructor instead, just in case textBox1 isn't edited first.
Related
Was following a tutorial on YouTube and tried to get my program to display the live video feed from my webcam into the forms picture box but for some reason the webcam comes on but the picture box isn't displaying anything
I have tried checking if the Mat class was returning null.
but it isn't this is my 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 Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.UI;
namespace FaceRecognitionAttandanceSystem
{
public partial class StudentAdd : Form
{
public string fileName { get; set; }
VideoCapture capture;
public StudentAdd()
{
InitializeComponent();
}
//Open Folder
private void metroButton3_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "JPEG|*.jpg", ValidateNames = true, Multiselect = false };
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//open file explorer
fileName = openFileDialog.FileName;
//pick image from file
Image img = Image.FromFile(fileName);
//Rotates Image by 90degrees
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
pictureBox2.Image = img;
}
}
}
//Capture Image
private void metroButton4_Click(object sender, EventArgs e)
{
//If capture is empty start new video capture
if(capture == null)
{
capture = new Emgu.CV.VideoCapture(0);
}
capture.ImageGrabbed += Capture_ImageGrabbed;
capture.Start();
}
private void Capture_ImageGrabbed(object sender, EventArgs e)
{
try
{
Mat mat = new Mat();
if(mat == null)
{
Console.WriteLine("Here you Go");
}
capture.Retrieve(mat);
pictureBox1.Image = mat.ToImage<Bgr, Byte>().ToBitmap<Bgr, Byte>();
}
catch(Exception)
{
}
}
private void metroButton5_Click(object sender, EventArgs e)
{
}
}
}
The problem is very likely that the Capture_ImageGrabbed event is raised on a background thread. And you can only update the UI on the UI thread. You can place a break point in the event-handler and check the thread-debug window to confirm. To fix this you need to move execution to the UI thread. Try:
capture.Retrieve(mat);
var bitmap = mat.ToImage<Bgr, Byte>().ToBitmap<Bgr, Byte>();
pictureBox1.Invoke(() => pictureBox1.Image = bitmap );
You might need to write Invoke((Action)(() => pictureBox1.Image = bitmap) ), since I think there is some issues with the overload resolution of lambdas with the invoke-method.
I created a c# program for convert image to binary,binary_inverse and such .
the porgram somehow only works for the image i used first but crashed whenever i tried other image .
I appreciate if someone could help me find the problem .
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 Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using Emgu.CV.CvEnum;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
resetui();
}
private Image<Bgr, Byte> ori;
private Image<Gray, Byte> edited;
private void button3_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pB.ImageLocation = openFileDialog1.FileName ;
ori = new Image <Bgr,Byte> (openFileDialog1.FileName) ;
edited = new Image<Gray, Byte>(ori.Width, ori.Height) ;
}
}
private void button2_Click(object sender, EventArgs e)
{
CvInvoke.cvCvtColor(ori, edited, COLOR_CONVERSION.CV_BGR2GRAY);
pB.Image = edited.ToBitmap();
groupBox1.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
pB.Image = ori.ToBitmap();
groupBox1.Enabled = false;
button1.Enabled = false;
button2.Enabled = true;
}
private void resetui()
{
button1.Enabled = true;
button2.Enabled = true;
groupBox1.Enabled = false;
}
private void CalcThresh()
{
if (cB.SelectedIndex == 0)
{
pB.Image = edited.ToBitmap();
return;
}
Image<Gray, byte> temp = new Image<Gray, Byte>(edited.Height, edited.Width);
double threshold = tB.Value;
double maxval = (double) max.Value;
THRESH mode = THRESH.CV_THRESH_BINARY;
switch (cB.SelectedIndex)
{
case 1:
mode = THRESH.CV_THRESH_BINARY ;
break ;
case 2:
mode = THRESH.CV_THRESH_BINARY_INV ; break ;
case 3:
mode = THRESH.CV_THRESH_TOZERO; break ;
case 4 :
mode = THRESH.CV_THRESH_TOZERO_INV; break ;
case 5:
mode = THRESH.CV_THRESH_TRUNC; break ;
}
CvInvoke.cvThreshold(edited, temp, threshold, maxval, mode );
pB.Image = temp.ToBitmap();
}
private void cB_SelectedIndexChanged(object sender, EventArgs e)
{
CalcThresh();
}
private void max_ValueChanged(object sender, EventArgs e)
{
CalcThresh();
}
private void tB_Scroll(object sender, EventArgs e)
{
CalcThresh();
}
}
}
Heres the first image i used
It works fine like this for example :
But for other image it crash whenever i picked a mode :
This one of the pic that made my program crashed :
This how the form deisgn looks like :
Im studying this for test so if someone can help me find the problem i really
appreciate it.
Thx Before
Replace this:
Image<Gray, byte> temp = new Image<Gray, Byte>(edited.Height, edited.Width);
With this:
Image<Gray, byte> temp = new Image<Gray, byte>(edited.Width, edited.Height);
I am making a desktop app in c# in which when the user selects specific name of background from the menu strip, then the background shall turn to that required background. The problem is that i cannot save the user input, i tried settings but i cannot find "system.drawing.image" in settings so is there any way i can save the user changed background ? No external backgrounds a user is allowed to change, just the ones in the resource folder. Here is my code which shows error that system.drawing.color cannot take place of drawing.image.
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 TAC
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
panel1.Location = new Point(165, 157);
panel2.Location = new Point(289, 158);
panel3.Location = new Point(47, 275);
panel4.Location = new Point(47, 402);
this.BackgroundImage = Properties.Settings.Default.FormImage;
}
private void bLUEToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackgroundImage = TAC.Properties.Resources.tex1;
}
private void gREENToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackgroundImage = TAC.Properties.Resources.tex2;
}
private void oRANGEToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackgroundImage = TAC.Properties.Resources.tex3;
}
private void rEDToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackgroundImage = TAC.Properties.Resources.tex4;
}
private void pURPLEToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackgroundImage = TAC.Properties.Resources.tex5;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.FormImage = this.BackgroundImage;
}
}
}
Use Save method to save settings
Properties.Settings.Default.Save();
If you want add an image to settings :
Add new setting with type string and use like this:
Save an image to setting ( when you close form)
MemoryStream ms = new MemoryStream();
Propertis.Resources.MyImage.Save(ms,ImageFormat.Jpeg);
Properties.Settings.Default.BackImg = Convert.ToBase64String(ms.ToArray());
Properties.Settings.Default.Save();
And read image from setting and set to background(in form load)
string img = Properties.Settings.Default.BackImg ;
byte[] i = Convert.FromBase64String(img);
this.BackgroundImage = Image.FromStream(new MemoryStream(i));
How add custom settings ?
http://www.codeproject.com/Articles/29130/Windows-Forms-Creating-and-Persisting-Custom-User
I'm currently having issues with my code in terms of getting a printing option to show up when thy click a specific button.
I am making a reminder program and was just experimenting. Also would like to know the most efficient way to add a daily notification system to my program
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 System.Net.Mail;
using System.IO;
using System.Drawing.Printing;
using System.Security;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
namespace simpleapp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add("Reminder: " + textBox1.Text);
}
private void input_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
listBox1.Items.Add("Reminder: " + textBox1.Text );
}
}
private void button2_Click(object sender, EventArgs e)
{
for (int rmd = listBox1.SelectedIndices.Count - 1; rmd >= 0; rmd--)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndices[rmd]);
}
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.SelectionMode = SelectionMode.MultiExtended;
}
private void button6_Click(object sender, EventArgs e)
{
FAQ faqs = new FAQ();
faqs.Show();
}
// When the Button is Clicked the List is saved in a ".txt file format for the user to view later
private void button3_Click(object sender, EventArgs e)
{
var Savingfileas = new SaveFileDialog();
Savingfileas.Filter = "Text (*.txt)|*.txt ";
if (Savingfileas.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
using (var Reminder = new StreamWriter(Savingfileas.FileName, false))
foreach (var item in listBox1.Items)
Reminder.Write(item.ToString() + Environment.NewLine);
MessageBox.Show("File has been successfully saved"+ '\n' + "Thank you for using the Remindr program");
}
}
private void button4_Click(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
Email_Client emc = new Email_Client();
emc.Show();
}
}
}
You have a few options. One is to graphically layout a print document and iterate through your listbox while drawing the text in the textbox using x and y coordinates. A better way would be to get your listbox items into a dataset and use it to generate a report. Are you storing these items in a database for retrieval? Here's a link that should help you get started. The tutorial is in VB.Net but there's only a small amount of code to this and should be easy to repeat using C# code.
Here is an absolutely minimal example of printing a ListBox.
It assumes your ListBox contains strings and shows a PrintPreviewDialog; it sets the page unit to mm, do pick a unit you are comfortable with..!
Of course you may chose one or more different fonts etc..
private PrintDocument document = new PrintDocument();
private void printButton_Click(object sender, EventArgs e)
{
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = document;
ppd.Document.DocumentName = "TESTING";
document.PrintPage += document_PrintPage;
ppd.ShowDialog();
}
void document_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.PageUnit = GraphicsUnit.Millimeter;
int leading = 5;
int leftMargin = 25;
int topMargin = 10;
// a few simple formatting options..
StringFormat FmtRight = new StringFormat() { Alignment = StringAlignment.Far};
StringFormat FmtLeft = new StringFormat() { Alignment = StringAlignment.Near};
StringFormat FmtCenter = new StringFormat() { Alignment = StringAlignment.Near};
StringFormat fmt = FmtRight;
using (Font font = new Font( "Arial Narrow", 12f))
{
SizeF sz = e.Graphics.MeasureString("_|", Font);
float h = sz.Height + leading;
for (int i = 0; i < listBox1.Items.Count; i++)
e.Graphics.DrawString(listBox1.Items[i].ToString(), font , Brushes.Black,
leftMargin, topMargin + h * i, fmt);
}
}
The actual printing is triggered when the user clicks the printer symbol in the dialog.
Note that there are more StringFormat options!
I was trying to convert an ink from Microsoft.Ink namespace to memorystream, so to convert it to image, but I don't understand why it's getting an error in the memorystream. I kind of felt that it was an error from Convert.FromBase64String()
But I don't know what other choices I have to convert it to image.
Please help me
Here is my 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;
using System.IO;
using Microsoft.Ink;
namespace testPaint
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
InkCollector ink;
private void Form1_Load(object sender, EventArgs e)
{
ink = new InkCollector(pictureBox1);
ink.Enabled = true;
ink.AutoRedraw = true;
}
private void btnSave_Click(object sender, EventArgs e)
{
UTF8Encoding utf8 = new UTF8Encoding();
ink.Enabled = false;
string strInk = Convert.ToBase64String(ink.Ink.Save(PersistenceFormat.Base64InkSerializedFormat, CompressionMode.Maximum));
textBox1.Text = strInk;
ink.Enabled = true;
}
private void btnClr_Click(object sender, EventArgs e)
{
ink.Enabled = false;
ink.Ink = new Microsoft.Ink.Ink();
ink.Enabled = true;
pictureBox1.Invalidate();
}
private void btnExport_Click(object sender, EventArgs e)
{
byte[] byImage = Convert.FromBase64String(textBox1.Text);
MemoryStream ms = new MemoryStream();
ms.Write(byImage, 0, byImage.Length);
Image img = Image.FromStream(ms);
img.Save("test.gif", System.Drawing.Imaging.ImageFormat.Gif);
ink.Enabled = true;
}
}
}
The documentation is very preliminary but I think you are probably using the wrong PersistenceFormat tag: you are using Base64 as the output format but you clearly want PersistenceFormat.Gif.
Apart from that, your conversion to and from a string is really not at all meaningful. Just use a private byte[] variable to store the ink data. Furthermore, your detour via a MemoryStream and a System.Graphics.Image makes no sense either.
// using System.IO;
private byte[] inkData;
private void btnSave_Click(object sender, EventArgs e)
{
inkData = ink.Ink.Save(PersistenceFormat.Gif, CompressionMode.Maximum);
}
private void btnExport_Click(object sender, EventArgs e)
{
// Data is already in GIF format, write directly to file!
using (var stream = new FileStream("filename", FileMode.Create, FileAccess.Write))
stream.Write(inkData, 0, inkData.Length);
}