Change the Display size of Image on Form resizing - c#

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;

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

Dynamically add pictures to Windows forms application

I am currently trying to insert a new PictureBox and show it with its associated image.I am creating PictureBox every time i open a new image. But it doesn't show after the dialog is closed. I am also trying to store the PictureBox objects in a list.
Here is the code I have written :
OpenFileDialog FileDlg = new OpenFileDialog();
FileDlg.Filter = "Image files (*.jpg, *.jpeg, *.png, *.bmp) | *.jpg; *.jpeg; *.png, *.bmp";
if (FileDlg.ShowDialog() == DialogResult.OK)
{
PictureBox picBox = new PictureBox();
picBox.Name = "PictureBox" + m_nPictureBoxCounter.ToString();
m_picboxList.Add(picBox);
picBox.Image = Image.FromFile(FileDlg.FileName);
picBox.BringToFront();
picBox.Location = new Point(10, 10);
picBox.Size = new Size(500, 500);
picBox.Visible = true;
}
You need to add picturebox into a control, i.e.:
this.controls.add(picBox);
OpenFileDialog FileDlg = new OpenFileDialog();
FileDlg.Filter = "Image files (*.jpg, *.jpeg, *.png, *.bmp) | *.jpg; *.jpeg; *.png, *.bmp";
if (FileDlg.ShowDialog() == DialogResult.OK)
{
var picBx = new PictureBox();
picBx.ImageLocation = FileDlg.FileName;
this.Controls.Add(picBx);
}
That should put you in the right place. You can adjust position and other properties as you see fit.
Thanks to Btc Sources i figured out the answer. I had to ad :
picBox.Parent = this;
picBox.BringToFront();

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

How To Save Image in My WPF Application

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

Categories