I'm using iconic.dll file to read data from compressed files (.zip file extensions)
Please check my code below
string zippath = txtFilePath.Text.Trim() + "\\" + foldername + ".zip";
ArrayList arrFiles = new ArrayList();
using (ZipFile zip = ZipFile.Read(enrollment))
{
foreach (ZipEntry e1 in zip)
{
arrFiles.Add(e1.ToString());
}
}
foreach (string path in arrFiles)
{
Image img1 = Image.FromFile(path); //geting error on this line
imageList.Images.Add(getThumbnaiImage(imageList.ImageSize.Width, img1));
}
How can I read image files from a compressed folder?
Try this :
using (ZipFile zip = ZipFile.Read(enrollment))
{
ZipEntry entry = zip["Image.bmp"];
entry.Extract(outputStream);
}
Also you can show your image in a pricturebox :
PictureBox pb = new PictureBox();
pb.Location = new Point(100, 100);
pb.SizeMode = PictureBoxSizeMode.Zoom;
var bmp = new Bitmap(outputStream);
pb.Image = bmp;
this.Controls.Add(pb);
This seems to be the solution:
using (ZipFile zip = ZipFile.Read(enrollment))
{
foreach (ZipEntry e1 in zip)
{
CrcCalculatorStream reader = e1.OpenReader();
MemoryStream memstream = new MemoryStream();
reader.CopyTo(memstream);
byte[] bytes = memstream.ToArray();
Image img1 = Image.FromStream(memstream);
imageList.Images.Add(getThumbnaiImage(imageList.ImageSize.Width, img1));
}
}
Related
i used PDFLIBNET to convert pdf to image :
public void ConvertPDFtoPNG(string filename, String dirOut)
{
try
{
PDFLibNet.PDFWrapper _pdfDoc = new PDFLibNet.PDFWrapper();
_pdfDoc.LoadPDF(filename);
System.Drawing.Image img = RenderPage(_pdfDoc, 0);
img.Save(Path.Combine(dirOut, Path.GetFileNameWithoutExtension(filename) + ".png"));
_pdfDoc.Dispose();
return;
}
catch
{
File.Copy(System.IO.Path.Combine(Environment.CurrentDirectory, "0.png"), Path.GetFileNameWithoutExtension(filename) + ".png");
}
}
This code is working properly
But i need to use in image handler without saving image
I change this code to use MemoryStream but get gray image :
public string ConvertPDFtoPNG(string filename)
{
PDFLibNet.PDFWrapper _pdfDoc = new PDFLibNet.PDFWrapper();
_pdfDoc.LoadPDF(filename);
System.Drawing.Image img = RenderPage(_pdfDoc, 0);
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
_pdfDoc.Dispose();
base64String = Convert.ToBase64String(ms.ToArray(), 0, ms.ToArray().Length);
}
please help me
thanks
Try this:
var b = File.ReadAllBytes(filename);
using (var ms = new MemoryStream(b))
{
var i = Image.FromStream(ms);
i.Save(ms, ImageFormat.Jpeg);
}
on buttonclick I'm taking image from file system and save into database, everything is ok but I want when I select image to display that image into pictureBox1
OpenFileDialog open = new OpenFileDialog() { Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg" };
if (open.ShowDialog() == DialogResult.OK)
{
txtPhoto.Text = open.FileName;
}
string image = txtPhoto.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();
byte[] Photo = bimage;
You can use Image property to set the Image for PictureBox Control.
Try This:
DialogResult result= openFileDialog1.ShowDialog();
if(result==DialogResult.OK)
pictureBox1.Image =new Bitmap(openFileDialog1.FileName);
if you want to add it in your code
Complete Code:
OpenFileDialog open = new OpenFileDialog() { Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg" };
if (open.ShowDialog() == DialogResult.OK)
{
txtPhoto.Text = open.FileName;
}
string image = txtPhoto.Text;
Bitmap bmp = new Bitmap(image);
pictureBox1.Image = bmp;//add this line
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();
byte[] Photo = bimage;
Simple code:
picturebox.Image = Bitmap.FromFile(yourimagepath);
OpenFileDialog open = new OpenFileDialog()
{
Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg"
};
if (open.ShowDialog() == DialogResult.OK)
{
PictureBoxObjectName.Image = Image.FromFile(open.FileName);
}
openFileDialog1.Multiselect = false;
openFileDialog1.Filter= "jpg files (*.jpg)|*.jpg";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
foreach (String file in openFileDialog1.FileNames)
{
picturebox1.Image = Image.FromFile(File);
}
}
This is for selection of one image.
I'm tryng to export images from docx file.
How can I convert EncodedPackage from Shape object as image?
Sample of my code:
DocumentFormat.OpenXml.Vml.Shape shape = imageElement.Descendants<DocumentFormat.OpenXml.Vml.Shape>().FirstOrDefault();
byte[] bytes = System.Convert.FromBase64String(shape.EncodedPackage.Value.Replace("\n", ""));
System.Drawing.Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = System.Drawing.Image.FromStream(ms);
}
image.Save(fileName);
You can get the image from ImagePart. Try this:
var imageParts = doc.MainDocumentPart.ImageParts;
foreach (var imagePart in imageParts)
{
var uri = imagePart.Uri;
var filename = uri.ToString().Split('/').Last();
var stream = doc.Package.GetPart(uri).GetStream();
Bitmap b = new Bitmap(stream);
b.Save(#"C:\Extracted_Images\" + filename);
}
I got a strange error with this program, It download a photo from internet, then I convert it to .jpeg and then I delete the first photo (in .png).
But i got an error: File is being used by another process. Why is happening this? I didn't open the file, and nobody is using it.
string outFile;
outFile = Path.GetTempFileName();
try
{
webClient.DownloadFile(foto, outFile);
if (foto.Substring(foto.Length - 3) == "png")
{
System.Drawing.Image image1 = System.Drawing.Image.FromFile(outFile);
foto = foto.Remove(foto.Length - 3) + "jpg";
string outFile2 = Path.GetTempFileName();
image1.Save(outFile2, System.Drawing.Imaging.ImageFormat.Jpeg);
System.IO.File.Delete(outFile);
outFile = outFile2;
}
}
FromFile is keeping the file open, you have to use something like this:
// Load image
FileStream filestream;
filestream = new FileStream("Filename",FileMode.Open, FileAccess.Read);
currentImage = System.Drawing.Image.FromStream(filestream);
filestream.Close();
The system.Drawing.Image is holding on to the file, Just wrap the image1 in a using statement.
string foto = "http://icons.iconarchive.com/icons/mazenl77/I-like-buttons/64/Style-Config-icon.png";
string outFile = Path.GetTempFileName();
WebClient webClient = new WebClient();
try
{
webClient.DownloadFile(foto, outFile);
if (Path.GetExtension(foto).ToUpper() == ".PNG")
{
string outFile2 = Path.GetTempFileName();
using (System.Drawing.Image image1 = System.Drawing.Image.FromFile(outFile))
{
image1.Save(outFile2, System.Drawing.Imaging.ImageFormat.Jpeg);
}
System.IO.File.Delete(outFile);
}
}
I have an image, person1.png, and four other images, person2.png, person3.png, person5.png, and person4.png. I want to rename these images in C# code. How would I do this?
Since the PNG files are in your XAP, you can save them into your IsolatedStorage like this:
//make sure PNG_IMAGE is set as 'Content' build type
var pngStream= Application.GetResourceStream(new Uri(PNG_IMAGE, UriKind.Relative)).Stream;
int counter;
byte[] buffer = new byte[1024];
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(IMAGE_NAME, FileMode.Create, isf))
{
counter = 0;
while (0 < (counter = pngStream.Read(buffer, 0, buffer.Length)))
{
isfs.Write(buffer, 0, counter);
}
pngStream.Close();
}
}
Here you can save it to whatever file name you want by changing IMAGE_NAME.
To read it out again, you can do this:
byte[] streamData;
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isfs = isf.OpenFile("image.png", FileMode.Open, FileAccess.Read))
{
streamData = new byte[isfs.Length];
isfs.Read(streamData, 0, streamData.Length);
}
}
MemoryStream ms = new MemoryStream(streamData);
BitmapImage bmpImage= new BitmapImage();
bmpImage.SetSource(ms);
image1.Source = bmpImage; //image1 being your Image control
Use the FileInfo.MoveTo method documented here. Moving a file to the same path but with a different name is how you rename files.
FileInfo fInfo = new FileInfo ("path\to\person1.png");
fInfo.MoveTo("path\to\newname.png")
If you need to manipulate paths, use the Path.Combine method documented here
On Windows Phone 7 the API methods to copy or move (rename) a file don't exist. (See http://msdn.microsoft.com/en-us/library/57z06scs(v=VS.95).aspx) You therefore have to do this yourself.
Something like:
var oldName = "file.old"; var newName = "file.new";
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var readStream = new IsolatedStorageFileStream(oldName, FileMode.Open, store))
using (var writeStream = new IsolatedStorageFileStream(newName, FileMode.Create, store))
using (var reader = new StreamReader(readStream))
using (var writer = new StreamWriter(writeStream))
{
writer.Write(reader.ReadToEnd());
}
store.DeleteFile(oldName);
}
When you upload image this function auto change the image name to Full date and return the full path where the image saved and with it new name.
string path = upload_Image(FileUpload1, "~/images/");
if (!path.Equals(""))
{
//use the path var..
}
and this is the function
string upload_Image(FileUpload fileupload, string ImageSavedPath)
{
FileUpload fu = fileupload;
string imagepath = "";
if (fileupload.HasFile)
{
string filepath = Server.MapPath(ImageSavedPath);
String fileExtension = System.IO.Path.GetExtension(fu.FileName).ToLower();
String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
try
{
string s_newfilename = DateTime.Now.Year.ToString() +
DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + fileExtension;
fu.PostedFile.SaveAs(filepath + s_newfilename);
imagepath = ImageSavedPath + s_newfilename;
}
catch (Exception ex)
{
return "";
}
}
}
}
return imagepath;
}
if you need more help i'll try :)