How To Save Image in My WPF Application - c#

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);

Related

Add an image into the resources file

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;
}
}

Display 4 picturebox 1 by 1

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;
}
}

C#: Using regular expression (Regex) to duplicate a specific character in a string

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;
}
}

strFileName in open file dialog

I am fairly new to c#.
My question is what is strFileName in open file dialog?
I have this code currently:
string input = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
strFileName = open.FileName;
if (strFileName == String.Empty)
return;
It brings up an error on strFileName. i cant find an explanation as to what it does in this code.
Any help will be appreciated and i apologise if this question has been asked before.
Without knowing what the error is, just by looking at your code, you are probably getting a compile error on strFileName because it is not declared:
You can either change your code to this:
string input = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
input = open.FileName;
if (input == String.Empty)
return;
Or this:
string strFileName = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
strFileName = open.FileName;
if (strFileName == String.Empty)
return;
It doesn't give me an error - though I did have to declare it. Did you?
string strFileName = ""; // added this line - it compiles and runs ok
private void TestFunc()
{
string input = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
strFileName = open.FileName;
if (strFileName == String.Empty)
return;
}
you need declare strFileName first
string strFileName = string.empty();
then use it.
You need to declare the variable strFilename as a string:
string strFileName = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
{
strFileName = open.FileName;
}
/* you probably don't want this unless it's part of a larger block of code
if (strFileName == String.Empty)
{
return;
}
*/
return strFileName;

Change the Display size of Image on Form resizing

I want that when i resize the form, Image Display resize with form size automatically
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";
if(openFileDialog1.ShowDialog() == DialogResult.OK){
pictureBox1.ImageLocation = openFileDialog1.FileName;
//pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
}
}
Suggest me some code please
try
pictureBox1.Dock = DockStyle.Fill;
and
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
Edit
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;

Categories