Re: Load Large Image (30Meg)
You should not get "out of memory" for a 30meg file, If you just created/uploaded the file maybe it is not released and a file sharing issue is happening. I'm guessing because more details are needed.......
Re: Load Large Image (30Meg)
Hey,
I don't know if this will be much help or not, but these are the methods that I use for resizing images on the fly, I can't vouch for it working on a image of that size though, I have never tried it:
Code:
private static byte[] ResizeImageFile(byte[] imageFile, int targetSize)
{
using (Image oldImage = Image.FromStream(new MemoryStream(imageFile)))
{
Size newSize = CalculateDimensions(oldImage.Size, targetSize);
using (Bitmap NewImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
{
using (Graphics canvas = Graphics.FromImage(NewImage))
{
canvas.SmoothingMode = SmoothingMode.AntiAlias;
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));
MemoryStream m = new MemoryStream();
NewImage.Save(m, ImageFormat.Jpeg);
return m.GetBuffer();
}
}
}
}
Code:
public override Stream ResizeImageFile(Stream imageFile, PhotoSize targetSize)
{
if (targetSize == PhotoSize.Original)
{
//Return pic without resizing
return imageFile;
}
else
{
Byte[] picBuff = StreamToBytes(imageFile);
switch (targetSize)
{
case PhotoSize.Large:
picBuff = ResizeImageFile(picBuff, 600);
break;
case PhotoSize.Medium:
picBuff = ResizeImageFile(picBuff, 198);
break;
case PhotoSize.Small:
picBuff = ResizeImageFile(picBuff, 100);
break;
}
return new MemoryStream(picBuff);
}
}
Code:
public enum PhotoSize
{
Small = 1, // 100px
Medium = 2, // 198px
Large = 3, // 600px
Original = 4 // Original Size
}
Code:
private static Size CalculateDimensions(Size oldSize, int targetSize)
{
Size newSize = new Size();
if (oldSize.Height > oldSize.Width)
{
newSize.Width = Convert.ToInt32(oldSize.Width * Convert.ToSingle(targetSize / Convert.ToSingle(oldSize.Height)));
newSize.Height = targetSize;
}
else
{
newSize.Width = targetSize;
newSize.Height = Convert.ToInt32(oldSize.Height * Convert.ToSingle(targetSize / Convert.ToSingle(oldSize.Width)));
}
return newSize;
}
Hope that might help!!
Gary
Re: Load Large Image (30Meg)
If this is happening right after an upload, chances are that the handle on the file hasn't been released, so you're attempting to read from a 0byte file.
If you need to manipulate it, do it straight from the byte array so that the file save and file manipulation happen separately.