Load undefined images from folders - c#

I saw many similar threads, but nothing that could help me solving my problem. I'm new to c# and I want to load 3 images into 3 picture boxes from 3 different folders on a form and print them later. The images are created through screenshots by a third party application and saved in those folders.
Nevertheless I can't define their names which is making problems with the file path, I think...
Instead of creating a SystemWatchFolder, I saw someone using:
open.Filter = "Image Files (*.png)|*.png"
Is that working in general or do I need a watch folder?
I tried to combine codes from similar projects and ended up with the one below (btw sorry for posting the whole code).
I also tried to change the path to: (#"C:....)
same error message.
I really need and appreciate your help, comments, thoughts etc.
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap("C:\\Users\\Public\\1-2d27a482-b755\\Files\\Snapshots\\Snapshot2\\");
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files (*.png)|*.png";
pictureBox1.Image = bmp;
}
private void PictureBox1_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap("C:\\Users\\Public\\1-2d27a482-b755\\Files\\Snapshots\\Snapshot2\\");
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files (*.png)|*.png";
pictureBox2.Image = bmp;
}
private void PictureBox2_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap("C:\\Users\\Public\\1-2d27a482-b755\\Files\\Snapshots\\Snapshot3\\");
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files (*.png)|*.png";
pictureBox3.Image = bmp;
}
private void PictureBox3_Click(object sender, EventArgs e)
The images in the boxes are not shown and I get this error:
System.ArgumentException: 'Parameter is not valid.'
for this line:
Bitmap bmp = new Bitmap("C:\\Users\\Public\\1-2d27a482-b755\\Files\\Snapshots\\Snapshot1\\");

What are you doing wrong
At first your Bitmap should take path to your picture. So it must be initialized at the end after user selects picture in OpenFileDialog. Also, you never opened your OpenFileDialog.
So all your methods should look look this:
private void PictureBox1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files (*.png)|*.png|All files (*.*)|*.*";
if (open.ShowDialog() == DialogResult.OK)
{
Bitmap bmp = new Bitmap(open.FileName);
pictureBox1.Image = bmp;
}
}
Better way to do this
You really don't need to create three similar methods that doing the same thing.
You can create only one and use it in all picture boxes:
private void PictureBox_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files (*.png)|*.png|All files (*.*)|*.*";
if (open.ShowDialog() == DialogResult.OK)
{
Bitmap bmp = new Bitmap(open.FileName);
PictureBox targetPictureBox = e.Source as PictureBox;
targetPictureBox.Image = bmp;
}
}

In the following code:
Bitmap bmp = new Bitmap("C:\\Users\\Public\\1-2d27a482-b755\\Files\\Snapshots\\Snapshot2\\");
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files (*.png)|*.png";
pictureBox2.Image = bmp;
there are two problems:
Problem 1
In the line:
Bitmap bmp = new Bitmap("C:\\Users\\Public\\1-2d27a482-b755\\Files\\Snapshots\\Snapshot2\\");
You are loading a Bitmap image from a file. The path:
"C:\\Users\\Public\\1-2d27a482-b755\\Files\\Snapshots\\Snapshot2\\"
is incorrect because it cant end with a \\.
It should end with Snapshot2 or Snapshot2.png depending on the name of the file.
Problem 2
In the lines:
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files (*.png)|*.png";
You are initializing an OpenFileDialog but not using it. You can get the file name by using a file dialogue or hard-code the file name as you did in the first line, but you are mixing theme up.
Option 1: Hard-coding the file name
This should work, assuming the file name is Snapshot2.png:
Bitmap bmp = new Bitmap("C:\\Users\\Public\\1-2d27a482-b755\\Files\\Snapshots\\Snapshot2.png");
pictureBox2.Image = bmp;
Option 2: Using a file dialogue
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files (*.png)|*.png";
if (open.ShowDialog() == DialogResult.OK)
{
pictureBox2.Image = new Bitmap(open.FileName);
}

Related

How to clear a pictureBox in C#? I have tried setting the property to null but it doesnt work

I would like to clear a pictureBox after clicking on an 'Encode' button to save it as a new image file in my project. I would like to clear the fields on the form after the user saves the image file.
The code I use to display image on pictureBox:
private void btnOpenfile_Click(object sender, EventArgs e)
{
// open file dialog
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files (*.png)|*.png";
if (open.ShowDialog() == DialogResult.OK)
{
// display image in picture box
pictureBox1.Image = new Bitmap(open.FileName);
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
tbFilepath.Text = open.FileName;
//pictureBox1.ImageLocation = tbFilepath.Text;
}
}
The code I used to clear the pictureBox:
private void clearForm()
{
pictureBox1.Image = null; //doesn't work
pictureBox1.Invalidate(); //doesn't work
tbFilepath.Text = "";
tbMessage.Text = "";
}
I have also tried the following but it did not work either:
private void clearForm()
{
Bitmap bm = new Bitmap(img);
bm.Save(tbFilepath.Text,System.Drawing.Imaging.ImageFormat.Png);
}
I tried using the Refresh() method as one of the commentors suggested, but it did not work either:
private void clearForm()
{
Refresh(); //first attempt
pictureBox1.Refresh();// second attempt
}
I expect the pictureBox field to clear out the existing image I have selected but the image did not clear away.
Before clicking on encode button
After clicking on encode button, the textBox fields are cleared but not the pictureBox field. I used the codes I have added in this question.
Try creating a new Bitmap Variable and use it for the picturebox:
Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
pictureBox.Image = bitmap;
pictrueBox.Invalidate();
Also, try to declare a Global Variable for the Bitmap so it is the one to be set as the picture and also the one to be cleared before setting it as the image for the PictureBox.
On the other hand you could try using the OnPaint Method of the Picurebox:
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}
Bitmap bm = null;
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
pictureBox1.Image = bm;
}
private void btn_Open_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp); *.PNG|*.jpg; *.jpeg; *.gif; *.bmp; *.PNG";
if (ofd.ShowDialog() == DialogResult.OK)
{
bm = new Bitmap(Image.FromFile(ofd.FileName), new Size(pictureBox1.Width, pictureBox1.Height));
textBox1.Text = ofd.FileName;
pictureBox1.Invalidate();
}
}
private void btn_Clear_Click(object sender, EventArgs e)
{
bm = null;
pictureBox1.Invalidate();
}
These code I made worked perfectly. So please try it.

WPF choosing an image via file dialog and interpolating it to a certain size

I'm trying to load up an image of my choosing in a WPF window and then change it's width and height. All I've come up with was:
public void OpenButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = false;
openFileDialog.Filter = "Image files (*.png;*.jpeg)|*.png;*.jpeg|All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == true)
{
ImageBox.Source= /*hard to miss lack of knowledge*/;
}
}
but I'm not sure it's the right way to do it. For the interpolation part I've found the method:
RenderOptions.SetBitmapScalingMode(ImageBox, BitmapScalingMode.HighQuality);
But the Microsoft docs didn't say anything about scaling it to a predefined height and width and I want to make it for example 200x200 if the given image is square.
Thanks in advance
Create a BitmapImage and set its DecodePixelWidth or DecodePixelHeight property:
if (openFileDialog.ShowDialog() == true)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(openFileDialog.FileName);
bitmap.DecodePixelHeight = 200;
bitmap.EndInit();
ImageBox.Source = bitmap;
}

Converting image with OpenfileDialog

I need help with this code. I want to create basic image convert program but this program is not working? What am I doing wrong. Thanks for answers.
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.ShowDialog();
string DosyaYolu = file.FileName;
string DosyaAdi = file.SafeFileName;
if (file.ShowDialog() == DialogResult.OK)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(DosyaYolu);
image.Save(DosyaYolu, System.Drawing.Imaging.ImageFormat.Png);
}
You choose the wrong target path to save the new image to. Also you invoked the ShowDialog() twice, which is not necessary. The following code will save the new file with the same name but a different extension.
var dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
string sourceFile = dialog.FileName;
string targetFile = Path.ChangeExtension(sourceFile, "png");
Image image = Image.FromFile(sourceFile);
image.Save(targetFile, ImageFormat.Png);
}

Load images in Image Button

I am using below code to load image in my image button. But the Image is not loading in Button. However, I'm not receiving any kind of error.
XAML Code:
Image Name="imgPhoto" HorizontalAlignment="Left" Height="160" Margin="10,41,0,0" VerticalAlignment="Top" Width="164"/>
Button Content="Load" HorizontalAlignment="Left" Margin="191,67,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
Below is the code to load the Image in Image Button.
Button Click Event Code:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Image files (*.jpg)|*.jpg|All Files (*.*)|*.*";
if (dlg.ShowDialog() == true)
{
string selectedFileName = dlg.File.Name;
imgPhoto.Source = new BitmapImage(new Uri(selectedFileName, UriKind.Relative));
}
}
with OpenFIleDialog we don't have File.Name we have only FileName property which Gets or sets a string containing the file name selected in the file dialog box.
check the below code
OpenFileDialog objOpenFileDialog = new OpenFileDialog();
objOpenFileDialog.Filter = "Image Files(.jpg)|*.jpg;*.gif;*.png";
if (objOpenFileDialog.ShowDialog() == true)
{
imgPhoto.Source =new BitmapImage(new Uri(objOpenFileDialog.FileName));
}
The problem is here: UriKind.Relative.
If you're building URI from file name, picked up from system's file dialog, then it is absolute URI:
myImage.Source = new BitmapImage(new Uri(fileDialog.FileName));
In other words, you should treat full path (e.g., "c:\folder\filename.ext") as absolute URI.
Change imgPhoto.Source code as below
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "Image files (*.jpg)|*.jpg|All Files (*.*)|*.*";
if (dlg.ShowDialog() == true)
{
string selectedFileName = dlg.FileName;
//imgPhoto.Source = new BitmapImage(new Uri(selectedFileName, UriKind.Relative));
imgPhoto.Source = (ImageSource)new ImageSourceConverter().ConvertFromString(selectedFileName);
}
}

How to save a picture in WPF application

Goal:
Save a picture from the harddrive into my WPF application. The picture should be available if copying the WPF application. The address to the picture located in the WPF application should be saved in the database.
Problem:
How should I do it in a course of action?
private void btnBrowse_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "jpg files (*.jpg)|*.jpg|gif files (*.gif)|*.gif|jpeg files (*.jpeg)|*.jpeg";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
// Open document
string filename = dlg.FileName;
txtPicture.Text = filename;
BitmapImage myBitmapImage = new BitmapImage(new Uri(dlg.FileName, UriKind.Absolute));
string sss = myBitmapImage.Format.ToString();
string asd = dlg.SafeFileName.ToString();
}
}
There are several image encoders available. A simple example for PNG files can be found here, or a more complete sample here. The same concept applies for the other supported image file types.

Categories