Dynamically add pictures to Windows forms application - c#

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

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

Multiple image thumbnail viewer

I am trying to create a simple winform application in c# that can load multiple images from a file dialog into the application screen. I am using ListView control to contain those images as picture box control wasn't going to serve my needs as I want to be able to select loaded thumbnails and do many other things based on the selection.
The problem I am having is to do with thumbnail image quality.
Once they get loaded in the application window panel, the image quality is very poor.
I want them to look just like how Windows operating system displays preview/thumbnails of images in large icon with a decent image quality.
I've tried adding this listView1.LargeImageList.ColorDepth = ColorDepth.Depth32Bit; as suggested by other people but it won't even let the app load images.
Here is the code done so far.
using (OpenFileDialog open = new OpenFileDialog())
{
open.Title = "Open Image";
open.Filter = "JPEG Files (*.jpg)|*.jpg;*.jpeg|All Files (*.*)|*.*";
open.Multiselect = true;
if (open.ShowDialog() == DialogResult.OK)
{
ImageList picList = new ImageList();
listView1.View = View.LargeIcon;
picList.ImageSize = new Size(200, 130);
foreach (String file in open.FileNames)
{
Image i = Image.FromFile(file);
Image pic = i.GetThumbnailImage(200, 130, null, new IntPtr());
picList.Images.Add(pic);
}
listView1.LargeImageList = picList;
listView1.LargeImageList.ColorDepth = ColorDepth.Depth32Bit;
for (int i = 0; i < picList.Images.Count; i++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = i;
listView1.Items.Add(item);
}
}
Does anyone have a suggestion as to what I should do to make it just as good as
Windows' default thumbnail viewer?

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

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