Multiple image thumbnail viewer - c#

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?

Related

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

uploading images in winforms application

Does anyone know if a there is a control to allow the user to upload a image to a windows form? Or any example code to accomplish this.
I am using win-form applications
Thanks,
To allow users to select files in a Windows Forms application you should look into using the OpenFileDialog class.
To use the dialog on your form you will need to find it in the toolbox in Visual Studio and drag it on to your form.
Once associated with the form you can then invoke the dialog from your code like so:
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string selectedFile = openFileDialog1.FileName;
}
You can then use the file path to perform whatever task you wish with the file.
Note: You can use the FileDialog.Filter Property to limit the type of file extensions (images in your case) the user can select when using the dialog.
It's note clear where you are going to upload your image. If you just want to use an image in a simple desktop application you can use OpenFileDialog to allow a user to select an image file. And then you can use this image path in you application. If you you want to upload this image to database you can read this image into memory using something like FileStream class.
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg";
if (open.ShowDialog() == DialogResult.OK)
{
textBox10.Text = open.FileName;
}
cn.Open();
string image = textBox10.Text;
Bitmap bmp = new Bitmap(image);
FileStream fs = new FileStream(image, FileMode.Open, FileAccess.Read);
byte[] bimage = new byte[fs.Length];
fs.Read(bimage, 0, Convert.ToInt32(fs.Length));
fs.Close();
SqlCommand cmd = new SqlCommand("insert into tbl_products(Product_image) values(#imgdata)", cn);
cmd.Parameters.AddWithValue("#imgdata", SqlDbType.Image).Value = bimage;
cmd.ExecuteNonQuery();
cn.Close();
private void cmdBrowser_Click(object sender, EventArgs e)
{
OpenFileDialog fileOpen = new OpenFileDialog();
fileOpen.Title = "Open Image file";
fileOpen.Filter = "JPG Files (*.jpg)| *.jpg";
if (fileOpen.ShowDialog() == DialogResult.OK)
{
picImage.Image = Image.FromFile(fileOpen.FileName);
}
fileOpen.Dispose();
}

How to display an image from file system in a PictureBox in WinForm

I have a small doubt regarding loading an image in PictureBox in WinForms.
I want to show an image file from file system in a PictureBox on my form, say form1.
I am doing Windows applications using C#.
I want to check the file type also say is it pdf/text/png/gif/jpeg.
Is it possible to programmatically open a file from file system using C#?
If anyone knows please give any idea or sample code for doing this.
Modified Code: I have done like this for opening a file in my system, but I don't know how to attach the file and attach the file.
private void button1_Click(object sender, EventArgs e)
{
string filepath = #"D:\";
openFileDialog1.Filter = "Image Files (*.jpg)|*.jpg|(*.png)|*.png|(*.gif)|*.gif|(*.jpeg)|*.jpeg|";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
try
{
}
}
}
I don't know what I have to write in try block. Can anyone help on this?
Use Image.ImageFromFile http://msdn.microsoft.com/en-us/library/system.drawing.image.fromfile.aspx method
Image img = Image.ImageFromFile(openFileDialog1.FileName);
Should work.
EDIT
If you're going to set it to PictureBox, and what to see complete inside it, use picturebox
SizeMode property.
using System.IO;
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = false; //not allow multiline selection at the file selection level
openFileDialog1.Title = "Open Data file"; //define the name of openfileDialog
openFileDialog1.InitialDirectory = #"Desktop"; //define the initial directory
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
try
{
string filename = openFileDialog1.FileName;
FileStream fs=new FileStream(filename, FileMode.Open, FileAccess.Read); //set file stream
Byte[] bindata=new byte[Convert.ToInt32(fs.Length)];
fs.Read(bindata, 0, Convert.ToInt32(fs.Length));
MemoryStream stream = new MemoryStream(bindata);//load picture
stream.Position = 0;
pictureBox1.Image = Image.FromStream(stream);
}
}

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.

c# saving an image of a control

what is the best way to save an image of a control?
currently i am doing this:
chart1.SaveImage(ms, ChartImageFormat.Bmp);
Bitmap bm = new Bitmap(ms);
how would i then prompt the user with a windowsavedialogue and save the BMP to a specific location?
if this is not the best way to do this please suggest a different way
Daok has a nice answer for this.
Adapting Daok's code to change the extension Filter gives you this
chart1.SaveImage(ms, ChartImageFormat.Bmp);
Bitmap bm = new Bitmap(ms);
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = Environment.SpecialFolder.MyDocuments;
saveFileDialog1.Filter = "Your extension here (*.bmp)|*.*" ;
saveFileDialog1.FilterIndex = 1;
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
bm.Save (saveFileDialog1.FileName);//Do what you want here
}
You could prompt them with a SaveFileDialog that would allow them to choose the path and file name and file type where they want to save the file.
Then you just need to write the bmp to a file
Do this:
SaveFileDialog dlg = new SaveFileDialog();
// ... add your dialog options
DialogResult result = dlg.ShowDialog(owner);
if(result == DialogResult.OK)
{
bm.Save(dlg.FileName);
}

Categories