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);
}
}
}
Related
I have the following code trying to compress a bitmap and use it for uploading.
Bitmap bitmap1 = BitmapFactory.DecodeByteArray(imageFileByte, 0, imageFileByte.Length);
byte[] compressedData = null;
using (var stream = new MemoryStream())
{
bitmap1.Compress(Bitmap.CompressFormat.Jpeg, 50, stream);
compressedData = stream.ToArray();
}
//load compressed bitmap from memory
using (var anotherStream = new MemoryStream(compressedData))
{
var compressedBitmap = BitmapFactory.DecodeStream(anotherStream);
}
When I debug, bitmap1 on line 1 shows 3021330 bytes, when come to the compressedData on line 6, it did shrink to 70128 bytes.
Now I want to convert again the compressedData to a bitmap again since it is a byte array but not a bitmap, when it comes to line 11, the compressedBitmap shows exactly same number of bytes with the bitmap1.
How can I actually use the bitmap that has already been compressed?
Is it possible to compress a bitmap and save the compressed one as a bitmap again?
Thanks for any comment.
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;
}
I have this code (C#):
unsafe private Bitmap Test()
{
Bitmap test = null;
byte[] data = memRenderAll.CurrentData;
fixed (byte* m_pBuffer = _data)
{
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
Bitmap a = new Bitmap(720, 576, 2160, System.Drawing.Imaging.PixelFormat.Format24bppRgb, new IntPtr(m_pBuffer));
a.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
test =(Bitmap) Image.FromStream(ms);
a.Dispose();
}
}
return _test;
}
By saving the stream as this:
a.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
I get a 10:1 reduction is size.
Is there a way of avoiding this:
a.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
and specifying it somewhere/somehow in:
Bitmap a = new Bitmap(720, 576, 2160, System.Drawing.Imaging.PixelFormat.Format24bppRgb, new IntPtr(m_pBuffer));
as I would like to create the Bitmap only once.
thanks,
Andrew
I think its impossible todo in one code line using standard System.Drawing.
You use that constructor: http://msdn.microsoft.com/en-us/library/zy1a2d14(v=vs.110).aspx
Your code:
new Bitmap(720, 576, 2160, System.Drawing.Imaging.PixelFormat.Format24bppRgb, new IntPtr(m_pBuffer));
What does it mean? Bitmap read memory pointed by m_pBuffer using fixed width, height, stride and pixel format. You can't read it as jpeg, because jpeg - image zipping fomat. Look on it: http://en.wikipedia.org/wiki/JPEG#JPEG_codec_example . Jpeg codec need all your image for ziping, it can't zip parts and join them after.
I'm saving both high resolution and compressed version of high resolution image in the database.
When the user requests a high resolution image, i need to display that else the compressed one. here is my code.
The issue is : when i set that image byte array into a stream and bitmap, file size has compressed 2.27MB to 339kB.
What i'm doing wrong here?
private void DisplayImageFromBytes(byte[] byteArray, int resizeWidth, bool isHiResImage) {
if (isHiResImage)
{
Stream stream = new MemoryStream(byteArray);
System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
Bitmap bitmap = null;
if (resizeWidth > 0 && img.Width > resizeWidth)
{
int newHeight = (int)((float)img.Height * ((float)resizeWidth / (float)img.Width));
bitmap = new Bitmap(img, resizeWidth, newHeight);
}
else
{
bitmap = new Bitmap(img);
}
Response.ContentType = "image/Jpeg";
bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Dispose();
img.Dispose();
bitmap.Dispose();
}
else
{
DisplayImageFromBytes(byteArray, resizeWidth);
}
}
You are using same image format to save it to stream for all the different format of images that came into you.
For now just replace
bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
with
bitmap.Save(Response.OutputStream, img.RawFormat);
RawFormat: will get the current file format of this Image.
N.B: You don't need to create a new bitmap that don't need any conversion. you can directly save it to stream from System.Drawing.Image img
With the way below i am able to read.
But there is no dispose method so i am not able to delete the file later.
So the below method is getting failed.
I could not come up with a proper solution.
Bitmap class is not recognized in C# 4.5 WPF application.
thank you
DirectoryInfo dInfo = new DirectoryInfo(#"C:\pokemon_files\images\");
FileInfo[] subFiles = dInfo.GetFiles();
BitmapImage myImg;
foreach (var vrImage in subFiles)
{
string srFilePath = vrImage.FullName;
System.Uri myUri = new Uri(srFilePath);
myImg = new BitmapImage(myUri);
if (myImg.Width < 50)
{
File.Delete(srFilePath);
continue;
}
}
I assume the error you get is caused by trying to delete the file which is currently in use
by the bitmap (I don't remember the exception name).
There is a solution to that, that is: making a byte stream.
byte[] imageData;
using(var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
using(var binaryReader = new BinaryReader(fileStream))
{
imageData = binaryReader.ReadBytes((int)fileStream.Length);
}
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = new MemoryStream(imageData);
bitmap.EndInit();
//Now you can check the width & height, the file stream should be closed so you can
//delete the file.
[EDIT]
If you don't want to read the bytes by BinaryReader, there's always this solution if you want to read all bytes from the file.