I have this code:
public diapo()
{
InitializeComponent();
DispatcherTimer messageTimer = new DispatcherTimer();
messageTimer.Tick += new EventHandler(messageTimer_Tick);
messageTimer.Interval = new TimeSpan(0, 0, 3);
messageTimer.Start();
}
private void source1(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Image files(*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
string sC = (openFileDialog.FileName);
}
private void source2(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog2 = new OpenFileDialog();
openFileDialog2.Filter = "Image files(*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
string sB = (openFileDialog2.FileName);
}
private void source3(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog3 = new OpenFileDialog();
openFileDialog3.Filter = "Image files(*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
string sA = (openFileDialog3.FileName);
}
I want to it switch pic.source (which is a image) every 3 seconds to the next string so sA, sB, sC
Related
I have a picture box where i display an image but i couldn't find the way to save it in the resources.
Here is my code so far:
private void imgContact_Click(object sender, EventArgs e)
{
// open file dialog
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
// display image in picture box
imgContact.Image = new Bitmap(open.FileName);
// image file path
string imagePath = open.FileName;
}
}
So I have a button and 4 pictureboxes and when I click the button I want to add on the first picturebox 1 picture and if I click the button the second time I want to make the picturebox2 = picturebox1 and the picturebox1 = the new image and so on until 4
This is what I did so far but it's not working, it shows me on the all 4 pictureboxes the same image:
namespace ImageUploadAndCameraUse
{
public partial class Form1 : Form
{
Image File;
Image File2;
Image File3;
Image File4;
bool button1Click = true;
bool button1Click2 = true;
bool button1Click3 = true;
bool button1Click4 = true;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog f = new OpenFileDialog();
f.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
bool IsNullOrEmpty1 = false;
bool IsNullOrEmpty2 = false;
bool IsNullOrEmpty3 = false;
if (f.ShowDialog() == DialogResult.OK)
{
if (button1Click)
{
File = Image.FromFile(f.FileName);
pictureBox1.Image = File;
IsNullOrEmpty1 = true;
button1Click = false;
}
if (IsNullOrEmpty1 && button1Click2 )
{
File2 = Image.FromFile(f.FileName);
pictureBox2.Image = pictureBox1.Image;
pictureBox1.Image = File2;
IsNullOrEmpty2 = true;
button1Click2 = false;
}
if (IsNullOrEmpty2 && button1Click3)
{
File3 = Image.FromFile(f.FileName);
pictureBox3.Image = File3;
IsNullOrEmpty3 = true;
button1Click3 = false;
}
if (IsNullOrEmpty3 && button1Click4)
{
File4 = Image.FromFile(f.FileName);
pictureBox4.Image = File4;
button1Click4 = false;
}
}
}
}
}
And also If you know: How can I make this program to use the device camera to take a photo if you dont have anything in any picturebox / the folder I will create to store all these photos.
You can just loop through your PictureBoxes to accomplish this:
int boxIndex = 0;
private void button1_Click(object sender, EventArgs e) {
OpenFileDialog f = new OpenFileDialog();
f.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
if (f.ShowDialog() == DialogResult.OK) {
PictureBox[] boxes = new PictureBox[] { pictureBox1, pictureBox2, pictureBox3, pictureBox4 };
if (boxIndex + 1 > boxes.Length) {
foreach (PictureBox pb in boxes) {
pb.Image = null;
}
boxIndex = 0;
}
for (int i = boxIndex; i > 0; --i) {
boxes[i].Image = boxes[i - 1].Image;
}
boxes[0].Image = Image.FromFile(f.FileName);
boxIndex++;
}
}
Try clone or instantiate new bitmap for PictureBox:
File2 = Image.FromFile(f.FileName);
pictureBox2.Image = new Bitmap(pictureBox1.Image); // or pictureBox1.Image.Clone()
pictureBox1.Image = File2;
You copy reference so you always show the same picture.
**EDIT
Try change your algorith because it is a bit complicated:
private List<Image> _images = new List<Image>();
private PictureBox[] _pictureBoxes = new [] { pictureBox1, pictureBox2, pictureBox3, pictureBox4 };
private void button1_Click(object sender, EventArgs e) {
var fileName = GetFileName();
if (string.IsNullOrEmpty(fileName)) return;
var image = Bitmap.FromFile(fileName);
_images.Insert(0, image); // I don't control items in list, you can remove items when reach count grater than 4
for(var i = 0; i < Math.Min(_images.Count, 4); i++) {
// _pictureBoxes[i].Image = null; // I'm not sure if this is necessary
_pictureBoxes[i].Image = _images[i]; // set image as we store it in list
}
}
private string GetFileName() {
var form = new OpenFileDialog();
form.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
return form.ShowDialog() == DialogResult.OK ? form.FileName : "";
}
Nevermind I solved it myself with a switch like that :
private void button1_Click(object sender, EventArgs e)
{
++NumberOfClick;
switch (NumberOfClick)
{
case 1:
OpenFileDialog f = new OpenFileDialog();
f.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
if (f.ShowDialog() == DialogResult.OK)
{
File = Image.FromFile(f.FileName);
pictureBox1.Image = File;
}
break;
case 2:
OpenFileDialog f2 = new OpenFileDialog();
f2.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
if (f2.ShowDialog() == DialogResult.OK)
{
File = Image.FromFile(f2.FileName);
pictureBox2.Image = pictureBox1.Image;
pictureBox1.Image = File;
}
break;
case 3:
OpenFileDialog f3 = new OpenFileDialog();
f3.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
if (f3.ShowDialog() == DialogResult.OK)
{
File = Image.FromFile(f3.FileName);
pictureBox3.Image = pictureBox2.Image;
pictureBox2.Image = pictureBox1.Image;
pictureBox1.Image = File;
}
break;
case 4:
OpenFileDialog f4 = new OpenFileDialog();
f4.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
if (f4.ShowDialog() == DialogResult.OK)
{
File = Image.FromFile(f4.FileName);
pictureBox4.Image = pictureBox3.Image;
pictureBox3.Image = pictureBox2.Image;
pictureBox2.Image = pictureBox1.Image;
pictureBox1.Image = File;
}
break;
default:
// other clicks
// . . .
break;
}
}
I am trying to code a quick program and the error shows up when I assign Properties.Settings to variable s.
I am trying to assign it to this variable because a lot of my text boxes need to be assigned with a value of the setting and also because there is a lot of settings which need saving.
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 SharpDock
{
public partial class SettingsWindow : Form
{
public Properties.Settings s = new Properties.Settings(); // This is the error.
public SettingsWindow()
{
InitializeComponent();
}
private void Settings_Load(object sender, EventArgs e)
{
app1.Text = s.app1;
app2.Text = s.app2;
app3.Text = s.app3;
app4.Text = s.app4;
app5.Text = s.app5;
app6.Text = s.app6;
ico1.Text = s.ico1;
ico2.Text = s.ico2;
ico3.Text = s.ico3;
ico4.Text = s.ico4;
ico5.Text = s.ico5;
ico6.Text = s.ico6;
}
private void abutton1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
app1.Text = ofd.FileName;
s.app1 = ofd.FileName;
}
}
private void Accept_Click(object sender, EventArgs e)
{
s.Save();
MessageBox.Show("SharpDock", "You must restart the program for the changes to take effect.", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void abutton2_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
app2.Text = ofd.FileName;
s.app2 = ofd.FileName;
}
}
private void abutton3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
app3.Text = ofd.FileName;
s.app3 = ofd.FileName;
}
}
private void abutton4_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
app4.Text = ofd.FileName;
s.app4 = ofd.FileName;
}
}
private void abutton5_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
app5.Text = ofd.FileName;
s.app5 = ofd.FileName;
}
}
private void abutton6_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
app6.Text = ofd.FileName;
s.app6 = ofd.FileName;
}
}
private void ibutton1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
ico1.Text = ofd.FileName;
s.ico1 = ofd.FileName;
}
}
private void ibutton2_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
ico2.Text = ofd.FileName;
s.ico2 = ofd.FileName;
}
}
private void ibutton3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
ico3.Text = ofd.FileName;
s.ico3 = ofd.FileName;
}
}
private void ibutton4_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
ico4.Text = ofd.FileName;
s.ico4 = ofd.FileName;
}
}
private void ibutton5_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
ico5.Text = ofd.FileName;
s.ico5 = ofd.FileName;
}
}
private void ibutton6_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
ico6.Text = ofd.FileName;
s.ico6 = ofd.FileName;
}
}
}
}
And the error is:
Error 1 Inconsistent accessibility: field type 'SharpDock.Properties.Settings' is less accessible than field 'SharpDock.SettingsWindow.s' C:\Users\Lewis\Documents\Visual Studio 2013\Projects\SharpDock\SharpDock\SettingsWindow.cs 14 36 SharpDock
Please help! I am stuck with this error.
~Lewis
You need to use Properties.Settings.Default and you don't need to instantiate a variable for it. If you want an alias to make the code smaller, remove this code:
public Properties.Settings s = new Properties.Settings();
And add this before the class declaration:
using s = Properties.Settings.Default;
And as deathismyfriend commented, you could reduce a lot your code by creating methods for the repeated code.
Your Settings are saved inside Properties.Settings.Default so you should use it instead of declaring your own settings. Also, you can subscribe multiple controls to the same event. What you should do is, subscribe all of your abuttons to aButton_Click event and ibuttons to iButton_Click event. Here is your code fixed:
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SharpDock
{
public partial class SettingsWindow : Form
{
public SettingsWindow()
{
InitializeComponent();
LoadSettings();
}
private void LoadSettings()
{
app1.Text = Properties.Settings.Default.app1;
app2.Text = Properties.Settings.Default.app2;
app3.Text = Properties.Settings.Default.app3;
app4.Text = Properties.Settings.Default.app4;
app5.Text = Properties.Settings.Default.app5;
app6.Text = Properties.Settings.Default.app6;
ico1.Text = Properties.Settings.Default.ico1;
ico2.Text = Properties.Settings.Default.ico2;
ico3.Text = Properties.Settings.Default.ico3;
ico4.Text = Properties.Settings.Default.ico4;
ico5.Text = Properties.Settings.Default.ico5;
ico6.Text = Properties.Settings.Default.ico6;
}
private void Accept_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Save();
MessageBox.Show("SharpDock", "You must restart the program for the changes to take effect.", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void aButton_Click(object sender, EventArgs e)
{
using (var ofd = new OpenFileDialog())
{
ofd.Filter = "Executable Files (*.exe) | *.exe";
ofd.Title = "Which executable would you like to launch?";
if (ofd.ShowDialog() == DialogResult.OK)
{
if (sender == abutton1)
{
app1.Text = ofd.FileName;
Properties.Settings.Default.app1 = ofd.FileName;
}
else if (sender == abutton2)
{
app2.Text = ofd.FileName;
Properties.Settings.Default.app2 = ofd.FileName;
}
else if (sender == abutton3)
{
app3.Text = ofd.FileName;
Properties.Settings.Default.app3 = ofd.FileName;
}
else if (sender == abutton4)
{
app4.Text = ofd.FileName;
Properties.Settings.Default.app4 = ofd.FileName;
}
else if (sender == abutton5)
{
app5.Text = ofd.FileName;
Properties.Settings.Default.app5 = ofd.FileName;
}
else if (sender == abutton6)
{
app6.Text = ofd.FileName;
Properties.Settings.Default.app6 = ofd.FileName;
}
}
}
}
private void iButton_Click(object sender, EventArgs e)
{
using (var ofd = new OpenFileDialog())
{
ofd.Filter = "Image Files (*.png) | *.png";
ofd.Title = "Which icon would you like?";
if (ofd.ShowDialog() == DialogResult.OK)
{
if (sender == ibutton1)
{
ico1.Text = ofd.FileName;
Properties.Settings.Default.ico1 = ofd.FileName;
}
else if (sender == ibutton2)
{
ico2.Text = ofd.FileName;
Properties.Settings.Default.ico2 = ofd.FileName;
}
else if (sender == ibutton3)
{
ico3.Text = ofd.FileName;
Properties.Settings.Default.ico3 = ofd.FileName;
}
else if (sender == ibutton4)
{
ico4.Text = ofd.FileName;
Properties.Settings.Default.ico4 = ofd.FileName;
}
else if (sender == ibutton5)
{
ico5.Text = ofd.FileName;
Properties.Settings.Default.ico5 = ofd.FileName;
}
else if (sender == ibutton6)
{
ico6.Text = ofd.FileName;
Properties.Settings.Default.ico6 = ofd.FileName;
}
}
}
}
}
}
You also need items that you're referencing from your settings to be set inside your settings:
Anyone know how to use regex to duplicate a specific character in a string?
I have a path that is entered like this:
C:/Example/example
I would like to use regex (or any other method) to display it like this:
C://Example//example
Is it possible?
This is where I'm getting the file path
private void btnSearchImage_Click_1(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string filenName = ofd.FileName;
pictureBox1.Image = new Bitmap(filenName);
string path = filenName;
txtimgPath.Text = path;
}
}
Thanks
How about this without RegEx?
var text = "C:/Example/example";
string outputValue = text.Replace("/","//"); //returns "C://Example//example"
This should work:
private void btnSearchImage_Click_1(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string filenName = ofd.FileName;
pictureBox1.Image = new Bitmap(filenName);
string path = filenName.Replace("/", "//");
txtimgPath.Text = path;
}
}
In My WPF application i am not able to save image inside my application in snap folder. Below is the code i am using.
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (ofd.ShowDialog() == DialogResult.OK)
{
string filepath = ofd.FileName;
File.Copy(ofd.FileName, Application.StartupPath + "\\snaps\\" + ofd.SafeFileName,true);
photoTextBox.Text= ofd.SafeFileName;
pictureBox1.Image = Image.FromFile(ofd.FileName);
}
Code to open the file browser
string filepath;
//browse Button
private void button4_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Multiselect = false;
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
bool? result = open.ShowDialog();
if (result == true)
{
filepath = open.FileName; // Stores Original Path in Textbox
ImageSource imgsource = new BitmapImage(new Uri(filepath)); // Just show The File In Image when we browse It
Clientimg.Source = imgsource;
}
}
And below is the code used for saving the file
private static String GetDestinationPath(string filename, string foldername)
{
String appStartPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
appStartPath = String.Format(appStartPath + "\\{0}\\" + filename, foldername);
return appStartPath;
}
How to use it
string name = System.IO.Path.GetFileName(filepath);
string destinationPath = GetDestinationPath(name,"YourFolderName");
File.Copy(filepath, destinationPath, true);