Save images from listview to a folder - c#

I had this listview and a button. There are also several images inside the listview. I want to save the images from the listview inside the folder when the button is pressed. And I don't know how to do it. Could you please help me out? Thanks. This is the code I use to insert images to the listview.
OpenFileDialog opend1 = new OpenFileDialog();
opend1.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
opend1.Multiselect = true;
if (opend1.ShowDialog() == DialogResult.OK)
{
listView1.View = View.LargeIcon;
imageList.ImageSize = new Size(100, 100);
for (int c = 0; c < opend1.FileNames.Length; c++)
{
Image i = Image.FromFile(opend1.FileNames[c].ToString());
Image img = i.GetThumbnailImage(100, 100, null, new IntPtr());
imageList.Images.Add(img);
}
listView1.LargeImageList = imageList;
ListViewItem lstItem = new ListViewItem();
lstItem.ImageIndex = imageList.Images.Count-1;
listView1.Items.Add(lstItem);
listView1.Refresh();
}

For each image in your image list (imageList.Images) call this (with your own supplied directory and file name):
img.Save(#"C:\MyImage.jpg", ImageFormat.Jpeg);

foreach (Image image in listView1.LargeImageList.Images)
{
string filename = ""; // make this whatever you need...
image.Save(filename);
}

Related

How to edit from the datagridview and save new changes

I have a resource file which i read the data and load it to the grid-view. now i want the user to be able to edit from the grid and click save button which will save it as a new file of resx? how do i do that? this is the code for reading the file.
oDataSet = new DataSet();
//now am reading the files from the path that is selected
XmlReadMode omode = oDataSet.ReadXml(PathSelection);
for (int i = 0; i < oDataSet.Tables[2].Rows.Count; i++)
{
string comment = oDataSet.Tables["data"].Rows[i][2].ToString();
string font = Between(comment, "[Font]","[/Font]");
string datestamp = Between(comment, "[DateStamp]", "[/DateStamp]");
string commentVal = Between(comment, "[Comment]", "[/Comment]");
string[] row = new string[]
{
oDataSet.Tables["data"].Rows[i][0].ToString(),
oDataSet.Tables["data"].Rows[i][1].ToString(),
font,
datestamp,
commentVal
};
Gridview_Input.Rows.Add(row);
cboLanguage.Enabled = true;
btnNewfile.Enabled = true;
}
You can try refresh your dataGridView each button click.
datagridviewName.update();
datagridviewName.refresh();

Out of memory exception when adding images to image list and displaying them into a list view

I am in the process to making an image gallery where i need to load more than 2 GB images from 1 directory to list view.
when i browse a folder above 200 mb its showing me the error OUT OF MEMORY.
my code is
_filenames = new List<string>();
DirectoryInfo dir = new DirectoryInfo(#root + "gallery");
foreach (FileInfo file in dir.GetFiles())
{
if (file.Name != "desktop.ini")
{
var image = Image.FromFile(file.FullName);
_filenames.Add(file.Name.ToLower());
imageList1.Images.Add(image);
}
else
{
break;
}
}
listView1.View = View.LargeIcon;
imageList1.ImageSize = new Size(75,75);
listView1.LargeImageList = imageList1;
for (int i = 0; i < imageList1.Images.Count; i++)
{
var item = new ListViewItem();
item.ImageIndex = i;
item.Text = _filenames[i];
listView1.Items.Add(item);
}
}
You need to (as a first), perform your image operations before adding them to the list:
Performing other operations, such as setting the ColorDepth or
ImageSize will cause the Handle to be recreated. Therefore, you should
perform these operations before you add images to the ImageList.
from MSDN
Update following clarifications:
In order to create your own file list then (assuming you have the directory name and file names within the directory) you could create a FileInfo[] array as follows:
// Prepare the directory and file names
var directoryName = "C:\\Temp\\MyFolder";
var filenames = new List<string>();
filenames.Add("0001.jpg");
filenames.Add("2345.jpg");
// Construct FileInfo array - using System.IO
var files = new FileInfo[filenames.Count];
for (var i = 0; i < filenames.Count; i++)
{
var fileName = filenames[i];
files[i] = new FileInfo(Path.Combine(directoryName, fileName));
}
There are many ways you could construct the FileInfo[] array (e.g. LINQ for example), but the above should work fine.

Display a Simpleitk image in a picture Box

I want to read a dicom or png image with simpleitk in a C# program and display the result in a pictureBox. I understand that picture Box allow only "system.drawing.image" and not itk. Is there a way to do it.
Her is my code :
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "PNG|*.png";
if (fd.ShowDialog() == DialogResult.OK)
{
string file = fd.FileName;
ImageFileReader reader = new ImageFileReader();
reader.SetFileName(file);
itk.simple.Image image = reader.Execute();
Box.Image = image;
}
You will need access to the raw image buffer which SimpleITK holds. This is accessible via the Image::GetBufferAs"TYPE" methods.
Here is a brief example on using this method:
// Cast so we know the the pixel type
input = SimpleITK.Cast(input, PixelId.sitkFloat32);
// calculate the nubmer of pixels
VectorUInt32 size = input.GetSize();
int len = 1;
for (int dim = 0; dim < input.GetDimension(); dim++) {
len *= (int)size[dim];
}
IntPtr buffer = input.GetBufferAsFloat();
I believe this can then be converted into to a Bitmap with .Net.
using (OpenFileDialog fd = new OpenFileDialog())
{
fd.Filter = "PNG|*.png";
if (fd.ShowDialog() == DialogResult.OK)
{
Box.Image = new Bitmap(fd.FileName);
}
}

How to display a different text for each image?

I have a problem about the foreach instruction and a description for each image.
Right now, when the user selects an image from the list, the next message occurs (see image1)
What I want is to change this text with a different one for each image.
I've also created a class called WineModel and its code is posted below.
What should I do to display a different description for each image?
One of my ideas would be to create a vector like in this link and to display a different message for each image from the position of the vector.
var files = Directory.GetFiles(#".\GalleryImages");
foreach (var file in files)
{
FileInfo fileInfo = new FileInfo(file);
WineModel wineModel = new WineModel();
wineModel.Image = new Uri(file, UriKind.Relative);
wineModel.Description = file + "Ana are mere" +
Environment.NewLine + "text text text text text text text text text text text";
wineModel.Price = new Random().NextDouble();
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = wineModel.Image;
bi.EndInit();
var button = new KinectTileButton
{
Label = System.IO.Path.GetFileNameWithoutExtension(file),
Background = new ImageBrush(bi),
Tag = wineModel
};
this.wrapPanel.Children.Add(button);
}
http://i58.tinypic.com/nbvhu8.png
http://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx

PDF file doesn't display in the listview c# 2008

I try to display the PDF files in the Listview as LargeIcon. But it doesn't display.
I can display the all image formats.
How to display the PDF file in the ListView?
I tried like this only, like image display.
private void import(string path)
{
ListView1.Items.Clear();
ImageList imageList = new ImageList();
imageList.Images.Clear();
imageList.ImageSize = new Size(170, 140);
imageList.ColorDepth = ColorDepth.Depth32Bit;
int i = 0;
////ArrayList to hold the files with the certain extensions
ArrayList files1 = new ArrayList();
if (Directory.Exists(path).Equals(true))
{
string validExtensions = "*.pdf";
//create a string array of our filters by plitting the
//string of valid filters on the delimiter
string[] extFilter = validExtensions.Split(new char[] { ',' });
//loop through each extension in the filter
foreach (string extension in extFilter)
{
files1.AddRange(Directory.GetFiles(path, extension));
files = (string[])files1.ToArray(typeof(String));
}
string[] pathes = new string[files.Length];
foreach (string file in files)
{
pathes[i] = file;
i++;
}
foreach (string path1 in pathes)
{
imageList.Images.Add(Bitmap.FromFile(path1));
FileInfo fileInfo = new FileInfo(path1);
String strDir = fileInfo.Name;
ListView1.Items.Add(strDir);
ListView1.TileSize = new System.Drawing.Size(100, 80);
}
for (int j = 0; j < pathes.Length; j++)
{
this.ListView1.Items[j].ImageIndex = j;
}
this.ListView1.View = View.LargeIcon;
this.ListView1.LargeImageList = imageList;
ListView1.Focus();
ListView1.Items[0].Selected = true;
}
}
}
You are going to need to rasterize the PDF. I think ImageMagick (maybe in conjunction with Ghostscript) can do this.
Disclaimer: I work for Atalasoft
We have a .NET library for Rasterizing PDF to an image available here:
http://atalasoft.com/products/dotimage/pdfrasterizer/
You need to find a .Net PDF renderer.
See this question.

Categories