I want to set picture box from zip file without extract zip.
my code:
ZipFile zip = new ZipFile("data.zip");
using (Stream s = zip["p3.png"].OpenReader())
{
picturebox.ImageLocation = s.ToString();
}
Picture Box Show error image.
I should use Bitmap. Correct Code:
ZipFile zip = new ZipFile("data.zip");
using (Stream s = zip["p3.png"].OpenReader())
{
Bitmap bitmap= new Bitmap(s);
PictureBox picturebox.Image = bitmap;
}
Related
I write this code to read files from folder in directory(#"D:\\test\\ISIC_2020_Training_JPEG"), then convert each file to bitmap in c#
foreach (string img in Directory.EnumerateFiles(#"D:\\test\\ISIC_2020_Training_JPEG"))
Bitmap bmp = new Bitmap(img);
But there is an error that appears in the last line, which is:
Out of memory Exception
what is the problem in this code?
I suppose that you have all jpeg files on provided directory, you could load image file in memory stream and check if everything okay when you load image in memory stream.
foreach (string imgPath in Directory.GetFiles(#"D:\test\ISIC_2020_Training_JPEG"))
{
Bitmap bmp;
byte[] buff = System.IO.File.ReadAllBytes(imgPath);
using(System.IO.MemoryStream ms = new System.IO.MemoryStream(buff))
{
bmp = new Bitmap(ms);
}
}
Probably the best approach would be to stream the image files so if there's a large file it won't hog up too much memory. Then check if the file is in the correct format before trying to convert to a Bitmap, hopefully this helps:
Bitmap bitmap;
Image image;
foreach (string imgFile in Directory.EnumerateFiles(#"D:\test\ISIC_2020_Training_JPEG"))
{
using (Stream bmpStream = File.Open(imgFile, FileMode.Open))
{
image = Image.FromStream(bmpStream);
if (ImageFormat.Jpeg.Equals(image.RawFormat)) // Check it's the correct format
{
bitmap = new Bitmap(image);
}
}
}
I'm using this code for converting Excel to image and preview it in a picturebox.
The code is working for first time. But when i'm trying to upload second time i get an error that says that the image file is in use.specically in save point.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "bak files (*.xls)|*.xls|All files (*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
Img1 = openFileDialog1.FileName;
//Create a new Workbook object and
//Open a template Excel file.
Workbook book = new Workbook(Img1);
//Get the first worksheet.
Worksheet sheet = book.Worksheets[0];
//Define ImageOrPrintOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
//Specify the image format
imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
//Only one page for the whole sheet would be rendered
imgOptions.OnePagePerSheet = true;
//Render the sheet with respect to specified image/print options
SheetRender sr = new SheetRender(sheet, imgOptions);
//Render the image for the sheet
Bitmap bitmap = sr.ToImage(0);
//Save the image file specifying its image format.
bitmap.Save("C:\\1.jpg");\\in this point i get my error that it says general error GDI+.
pictureBox2.Image = Image.FromFile("C:\\1.jpg");
pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
}
else
{
this.DialogResult = DialogResult.None;
}
}
I thing i must stop first use from my picturebox preview and then to upload again. But how will i do that? I tried
pictureBox1.Image=null
,but it didnt worked.
When you use
pictureBox2.Image = Image.FromFile("C:\\1.jpg");
The static call keeps the file open (and locked). So you can't overwrite the file.
Solution:
if (pictureBox2.Image != null) pictureBox2.Image.Dispose();
bitmap.Save("C:\\1.jpg");
bitmap.Dispose();
pictureBox2.Image = new Bitmap(Image.FromFile("C:\\1.jpg"));
This way the file is released and can later be overwritten.
An alternative:
if (pictureBox2.Image != null) pictureBox2.Image.Dispose();
bitmap.Save("C:\\1.jpg");
bitmap.Dispose();
using (Bitmap bm = new Bitmap(C:\\1.jpg"))
{
pictureBox2.Image = new Bitmap(bm);
};
I solved this using FileStream.
Dim fs1 As System.IO.FileStream
fs1 = New System.IO.FileStream(Bitmap, IO.FileMode.Open, IO.FileAccess.Read)
PictureBox1.Image = System.Drawing.Image.FromStream(fs1)
fs1.Close()
Look that you should fs1.close() to finish the stream.
The FileStream Class represents a File in the machine. FileStream allows to move data to and from the stream as arrays of bytes. It means, it does'nt works directly in the file, just as a Stream that you can manipulate.
The code is in vb.net (because it works for me).
So there will be no problem translating it to C #
Here VB.NET Example for opening Image in picture box without keeping file locked for other operation.
VB.NET Code.
Dim fs As System.IO.FileStream
' Specify a valid picture file path on your computer.
fs = New System.IO.FileStream("C:\WINNT\Web\Wallpaper\Fly Away.jpg",
IO.FileMode.Open, IO.FileAccess.Read)
PictureBox1.Image = System.Drawing.Image.FromStream(fs)
fs.Close()
C# Code.
System.IO.FileStream fs;
// Specify a valid picture file path on your computer.
fs = new System.IO.FileStream(#"C:\WINNT\Web\Wallpaper\Fly Away.jpg", System.IO.FileMode.Open, System.IO.FileAccess.Read);
PictureBox1.Image = System.Drawing.Image.FromStream(fs);
fs.Close();
Reference -
https://www.codeproject.com/Questions/492654/5bc-23-5dplusdeleteplusimagepluswhichplusisplusope
I have WPF and C# application. which captures the images and Save in to file(*.jpg).
I have the image path and i want to rotate image saved in File through the c# code.
and Save the Rotated image in same file.
How can i do that?
Use the rotate flip method.
E.g.:
Bitmap bitmap1 = (Bitmap)Bitmap.FromFile(#"C:\test.jpg");
bitmap1.RotateFlip(RotateFlipType.Rotate180FlipNone);
bitmap1.Save(#"C:\Users\Public\Documents\test rotated.jpg");
you can use my method:
BitmapImage rotateImage(string filename,int angle)
{
WIA.ImageFile img = new WIA.ImageFile();
img.LoadFile(filename);
WIA.ImageProcess IP = new WIA.ImageProcess();
Object ix1 = (Object)"RotateFlip";
WIA.FilterInfo fi1 = IP.FilterInfos.get_Item(ref ix1);
IP.Filters.Add(fi1.FilterID, 0);
Object p1 = (Object)"RotationAngle";
Object pv1 = (Object)angle;
IP.Filters[1].Properties.get_Item(ref p1).set_Value(ref pv1);
img = IP.Apply(img);
File.Delete(filename);
img.SaveFile(filename);
BitmapImage imagetemp = new BitmapImage();
using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
imagetemp.BeginInit();
imagetemp.CacheOption = BitmapCacheOption.OnLoad;
imagetemp.StreamSource = stream;
imagetemp.EndInit();
}
return imagetemp;
}
usage:
string filename = System.AppDomain.CurrentDomain.BaseDirectory + "4.jpg";
image.Source = rotateImage(filename,90);
I am trying to store some soundclips which are tagged with Images chosen from ISO Storage through a PhotoChooserTask.
I can successfully display the image in a standalone Image box but when I am setting the Imagebox source inside a listbox It does not shows the image.
Currently what I am doing is something like this:
public ImageSource Image
{
get {
try
{
BitmapImage image;
if(Category == 11)
{
image = new BitmapImage(new Uri(this.ImageLocation));
}
return image;
}
catch (Exception)
{
return null;
}
}
I don't understand what is missing
BitmapImage can't load image from Isolated Storage. You need to read file image manually
BitmapImage bi = new BitmapImage();
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(this.ImageLocation, FileMode.Open, FileAccess.Read))
{
bi.SetSource(fileStream);
}
}
return bi;
Also, if this won't work, check CreateOptions and set it to None
I am using C# and WPF. I want to add a PNG and JPG at runtıme to an image source but I get an exception that says:
Can not implicitly add convert type string to Sytem.Windows.Media.imageSource
using System.IO; //for : input - output
using Microsoft.Win32; //For : OpenFileDialog / SaveFileDialog
using System.Windows.Media.Imaging; //For : BitmapImage etc etc
<Image x:Name="img" Margin="9,13.5,6,0.5" Source="Laugh.ico">
private void ac(object sender, RoutedEventArgs args)
{
OpenFileDialog dlg = new OpenFileDialog();
// Configure open file dialog box
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".PNG"; // Default file extension
dlg.Filter = " (.PNG)|*.PNG"; // Filter files by extension
// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process open file dialog box results
if (result == true)
{
// Open document
string filename = dlg.FileName;
img.Source=filename;
}
}
Sorry but as you might have noticed, that code wonT even compile. You cannot set the image source as the filename
img.Source = filename
Have a look at the reference.
Try this:
img.Source = new BitmapImage(new Uri(filename));
Not sure if this will work for an image outside of the program, but you can try:
Uri uri = new Uri(dlg.File.FullName, UriKind.RelativeOrAbsolute);
ImageSource imgSource = new BitmapImage(uri);
img.Source = imgSource;
I think the key line is:
img.Source=filename
which should be something like:
BitmapImage bi= new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(filename, UriKind.Relative);
bi.EndInit();
img.Source = bi;
As you have to actually read the image file in from disk.