Graphics.DrawImage() - Throws out of memory exception - c#

I have some images that I need to do some crude re-size work on -- For the purpose of this example lets just say I need to increase the width and height of a given image by 4 pixels.
I am unsure why the call to Graphics.DrawImage() is throwing an OOM -- Any advice here would be greatly appreciated.
class Program
{
static void Main(string[] args)
{
string filename = #"c:\testImage.png";
// Load png from stream
FileStream fs = new FileStream(filename, FileMode.Open);
Image pngImage = Image.FromStream(fs);
fs.Close();
// super-hacky resize
Graphics g = Graphics.FromImage(pngImage);
g.DrawImage(pngImage, 0, 0, pngImage.Width + 4, pngImage.Height + 4); // <--- out of memory exception?!
// save it out
pngImage.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
}
}

I just had the same problem. However fixing the size of the output Graphics did not solve my problem. I realized that I tried to use a very high quality for the drawing the image which was consuming too much memory when I use the code on a lot of images.
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
after commenting these lines out the code worked perfectly.

Your Graphics surface is only big enough for the original-sized image. You need to create a new image of the correct size and use it as the source of your Graphics object.
Image newImage = new Bitmap(pngImage.Width + 4, pngImage.Height+4);
Graphics g = Graphics.FromImage(newImage);

That likely will not accomplish what you'd like to do seeing as how the image is the same size as the one specified by FromImage, instead you can use the Bitmap class:
using (var bmp = new Bitmap(fileName))
{
using (var output = new Bitmap(
bmp.Width + 4, bmp.Height + 4, bmp.PixelFormat))
using (var g = Graphics.FromImage(output))
{
g.DrawImage(bmp, 0, 0, output.Width, output.Height);
output.Save(outFileName, ImageFormat.Png);
}
}

Can you try this fix?
class Program
{
static void Main(string[] args)
{
string filename = #"c:\testImage.png";
// Load png from stream
FileStream fs = new FileStream(filename, FileMode.Open);
Image pngImage = Image.FromStream(fs);
fs.Close();
// super-hacky resize
Graphics g = Graphics.FromImage(pngImage);
pngImage = pngImage.GetThumbnailImage(image.Width, image.Height, null, IntPtr.Zero);
g.DrawImage(pngImage, 0, 0, pngImage.Width + 4, pngImage.Height + 4); // <--- out of memory exception?!
// save it out
pngImage.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
}
}
Inspired by this question: Help to resolve 'Out of memory' exception when calling DrawImage

Related

C# CopyFromScreen multiple times to same file

I am trying to take multiple screenshots and save them to a file. However, screenshots are taken decently often, and in order to not lose any of them, my current program will simply create a new file for every screenshot. Ideally, the program would simply "append" the most recent screenshot onto a single file every time.
Here is the code:
static Rectangle bounds = Screen.GetBounds(Point.Empty);
static Size rectSize = new Size(bounds.Width, bounds.Height);
public static void takeScreenshot(string path, int iteration, string filetype)
{
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(Point.Empty, Point.Empty, new Size
(rectSize.Width * iteration, rectSize.Height));
}
bitmap.Save(path + filetype);
}
}
iteration was the amount of times the method has been called. I was trying to just shift the next screenshot over by one screenshot's width while keeping every other screenshot, but it seems to overwrite the file anyways. Is it possible to do this?
Try this..
string n = string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now);
bitmap.Save(path + n + filetype);
Like TheGeneral said, you will run out of memory quickly. And every time the file grows it has a chance of needing to be moved on disc wearing down your drives.
Also, opening and viewing huge image files can be very slow, your computer will not be able to handle it very quickly.
But here it is, maybe at least the AppendImage function could be used for something like generating particle sprite strips in games or something.
If you're appending a screenshot, I wouldn't append more than maybe 10 times, with 1080P monitor.
public enum AppendLocation
{
Before,
After
}
void AppendScreenToFile(string filename, AppendLocation appendLocation = AppendLocation.After)
{
Rectangle bounds = Screen.PrimaryScreen.Bounds;
using (Bitmap screenShot = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(screenShot))
{
g.CopyFromScreen(UpperLeftSource, Point.Empty, bounds.Size);
}
if (!File.Exists(filename))
{
screenShot.Save(filename, ImageFormat.Png);
return;
}
//Not using Image.FromFile as it blocks saving.
Image onDisc;
using (Stream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
onDisc = Image.FromStream(fs);
using (Image appended = appendLocation == AppendLocation.Before ? AppendImage(screenShot, onDisc) : AppendImage(onDisc, screenShot))
appended.Save(filename, ImageFormat.Png);
}
}
public enum AppendAxis
{
Vertical,
Horizontal
}
Bitmap AppendImage(Image image, Image append, AppendAxis axis = AppendAxis.Vertical)
{
Bitmap bitmap;
Rectangle destinationRect;
RectangleF imageBounds = new Rectangle(0, 0, image.Width, image.Height);
RectangleF appendRect = new Rectangle(0, 0, append.Width, append.Height);
pictureBox_Item4.BackgroundImage = image;
pictureBox_Item3.BackgroundImage = append;
switch (axis)
{
case AppendAxis.Vertical:
destinationRect = new Rectangle(0, image.Height, append.Width, append.Height);
bitmap = new Bitmap(image.Width, image.Height + append.Height);
break;
case AppendAxis.Horizontal:
destinationRect = new Rectangle(image.Width, 0, append.Width, append.Width);
bitmap = new Bitmap(image.Width + append.Width, image.Height);
break;
default:
throw new ArgumentException("AppendAxis is invalid.");
}
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(image, imageBounds, imageBounds, GraphicsUnit.Pixel);
g.DrawImage(append, destinationRect, appendRect, GraphicsUnit.Pixel);
return bitmap;
}
}

Out of memory when cropping a bitmap with .Clone()

I am trying to automatically generate a thumbnail from an image uploaded by a user but I keep getting the exception "Out of memory".
From what I understand the out of memory exception is thrown when you specify a starting position or a width/height that's outside of the image but even if I do this
var rct = new Rectangle(5, 5, 10, 10);
var whatever = bitmap.Clone(rct, bitmap.PixelFormat);
on an image that is 800x900 pixels I still get the "Out of memory" exception, I can't figure out what's wrong with it and I can't really get any good answers from other threads since everything regarding the OOM exception is just the mistake of going outside the image boundaries.
Does anyone have an explanation or solution to this?
EDIT: A bit more context
The loop for the images.
foreach (var blob in fileInfoList)
{
var blockBlobName = CheckExistence(BaseBlobUrl, blob.FileName, blob.FileNameWithoutExtension);
var image = new Image()
{
BlobUrl = Path.Combine(BaseBlobUrl, blockBlobName),
FullName = blob.FileName,
FileName = blob.FileNameWithoutExtension,
BlockBlobName = blockBlobName,
OwningOrganizationId = CurrentUser.UserOrganization.OrganizationId,
ThumbnailUrl = CreateThumbnail(blob.File, blockBlobName),
Name = "Whatever"
};
blobList.Add(image);
RepositoryFactory.AzureStorageRepository.SaveImage(blob.File, blockBlobName, blob.ContentType, CurrentUser.UserOrganization.Organization.Id);
}
The method that is being called by each image in the list to generate the thumbnail.
public string CreateThumbnail(byte[] b, string parentImageName)
{
Bitmap bmp;
using (var ms = new MemoryStream(b))
{
bmp = new Bitmap(ms);
}
Bitmap thumbnail = bmp;
Rectangle rect = new Rectangle(5, 5, 10, 10);
if (bmp.Width > bmp.Height)
thumbnail = bmp.Clone(rect, bmp.PixelFormat);
else if (bmp.Height > bmp.Width)
thumbnail = bmp.Clone(new Rectangle((bmp.Height/2) - (bmp.Width/2), 0, bmp.Width, bmp.Width), bmp.PixelFormat);
byte[] bmpArray = new byte[0];
using (var ms = new MemoryStream())
{
finalCrop.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Close();
bmpArray = ms.ToArray();
}
var name = "Thumbnail_" + parentImageName;
RepositoryFactory.AzureStorageRepository.SaveThumbnail(bmpArray, name, "jpg/image", CurrentUser.UserOrganization.Organization.Id);
return BaseBlobUrl + "thumbnails/" + name;
}
I think the problem you're getting here is that Bitmaps need to be disposed. If one gets garbage collected without releasing its underlying unmanaged content (i.e. being disposed), then that memory can not be recovered...
Also note that you will need to dispose both bitmaps. Best thing to do is wrap them in a using, something like this:
using (var ms = new MemoryStream(b))
{
using (Bitmap bmp = new Bitmap(ms))
using (Bitmap thumbnail = bmp)
{
Rectangle rect = new Rectangle(5, 5, 10, 10);
if (bmp.Width > bmp.Height)
thumbnail = bmp.Clone(rect, bmp.PixelFormat);
else if (bmp.Height > bmp.Width)
thumbnail = bmp.Clone(new Rectangle((bmp.Height / 2) - (bmp.Width / 2), 0, bmp.Width, bmp.Width), bmp.PixelFormat);
byte[] bmpArray = new byte[0];
using (var ms = new MemoryStream())
{
finalCrop.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Close();
bmpArray = ms.ToArray();
}
var name = "Thumbnail_" + parentImageName;
RepositoryFactory.AzureStorageRepository.SaveThumbnail(bmpArray, name, "jpg/image", CurrentUser.UserOrganization.Organization.Id);
return BaseBlobUrl + "thumbnails/" + name;
}
}
It is worth noting that using will call Dispose() on it's target, even if an exception is thrown (thus having the same finally type functionality as #Scott Chamberlain answer
Okay so I found an answer to my problem by disposing the Bitmaps.
After this bit of code
Bitmap thumbnail = bmp;
I added
bmp.Dispose();
And during debugging I noticed that none of the properties from bmp were left in the Bitmap called thumbnail so I changed it into the following
Bitmap thumbnail = new Bitmap(bmp);
Thank you all for telling me to dispose the Bitmaps!
Here is the correct way to properly dispose of your objects.
Bitmap bmp = null;
Bitmap thumbnail = null;
try
{
using (var ms = new MemoryStream(b))
{
bmp = new Bitmap(ms);
}
Rectangle rect = new Rectangle(5, 5, 10, 10);
if (bmp.Width > bmp.Height)
thumbnail = bmp.Clone(rect, bmp.PixelFormat);
else if (bmp.Height > bmp.Width)
thumbnail = bmp.Clone(new Rectangle((bmp.Height/2) - (bmp.Width/2), 0, bmp.Width, bmp.Width), bmp.PixelFormat);
else
thumbnail = bmp;
byte[] bmpArray = new byte[0];
using (var ms = new MemoryStream())
{
finalCrop.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Close();
bmpArray = ms.ToArray();
}
var name = "Thumbnail_" + parentImageName;
RepositoryFactory.AzureStorageRepository.SaveThumbnail(bmpArray, name, "jpg/image", CurrentUser.UserOrganization.Organization.Id);
return BaseBlobUrl + "thumbnails/" + name;
}
finally
{
if(bmp != null)
bmp.Dispose();
if(thumbnail != null)
thumbnail.Dispose(); //If bmp and thumbnail are the same object this is still safe to do.
}
Use a try/finally block to ensure that even in the event of a error your objects get disposed. Doing the extra Bitmap thumbnail = new Bitmap(bmp); in your answer just makes a extra bitmap you are forgetting to dispose.
I received this Exception when the Rectangle used to crop the image was partly outside the bounds of the image.

How to save a bitmap after setting interpolation with graphics class

This code resizes an image and saves it to disk.
using (var medBitmap = new Bitmap(fullSizeImage, newImageW, newImageH))
{
medBitmap.Save(HttpContext.Current.Server.MapPath("~/Media/Items/Images/" + itemId + ".jpg"),
ImageFormat.Jpeg);
}
But if I want to use the graphics class to set the interpolation, how do I save it? The graphics class has a save method, but it doesn't take any parameters. How do I save it to disk like the bitmap? Heres a modified code snippet:
using (var medBitmap = new Bitmap(fullSizeImage, newImageW, newImageH))
{
var g = Graphics.FromImage(medBitmap);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//What do I do now?
medBitmap.Save(HttpContext.Current.Server.MapPath("~/Media/Items/Images/" + itemId + ".jpg"),
ImageFormat.Jpeg);
}
I just need to set the interpolation and then save it to disk.
Call DrawImage on the Graphics object to update the bitmap:
using (var medBitmap = new Bitmap(fullSizeImage, newImageW, newImageH))
{
using (var g = Graphics.FromImage(medBitmap))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(medBitmap, 0, 0);
}
medBitmap.Save(HttpContext.Current.Server.MapPath("~/Media/Items/Images/" + itemId + ".jpg"), ImageFormat.Jpeg);
}
Create a new Bitmap with the size you want and set the interpolationMode. Then use Graphics.DrawImage to draw the full sized image into the new bitmap.

Image resizing with GDI+

I'm really trying to nail out a little more performance out of this tidbit of code. It's not a heavly used bit of code but is used every time a new image is uploaded, and 4 times for each image (100px, 200px, 500px, 700px). So when there are any more than 2 or 3 images processing, it gets a little busy on the server. Also I'm trying to figure out how to make it correctly process images with a low resolution. Currently it just chops it off half way through, not plesent.
Examples: Original, large, xLarge
public static byte[] ResizeImageFile(byte[] imageFile, int targetSize)
{
using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
{
Size newSize = CalculateDimensions(oldImage.Size, targetSize);
using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppRgb))
{
newImage.SetResolution(oldImage.HorizontalResolution, oldImage.VerticalResolution);
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();
}
}
}
}
private static Size CalculateDimensions(Size oldSize, int targetSize)
{
Size newSize = new Size();
if (oldSize.Width > oldSize.Height)
{
newSize.Width = targetSize;
newSize.Height = (int)(oldSize.Height * (float)targetSize / (float)oldSize.Width);
}
else
{
newSize.Width = (int)(oldSize.Width * (float)targetSize / (float)oldSize.Height);
newSize.Height = targetSize;
}
return newSize;
}
Thanks for and help!
The first thought that comes to mind is, have you thought about Multithreading it? i.e. calling this method for each image (or batch of images) in a separate thread? That way, if your server has a few cores you can get things done quicker. Just a thought...
(Threading is a great tip.)
Try to call your method with the smallest possible image as input each time, instead of the original image. If the original image is, say 2000px, then create the 700px image from it and then use your newly created 700px image to create the 500px, etc...
With the HighQualityBicubic setting I doubt that you'll notice any difference in the 100px image. (But it of course it needs to be verified.)
For completeness, here is the solution to the second part of the question which was never answered. When processing a low resolution image the image was being cut off. The solution now, seems obvious. The problem lies in this bit of code from above:
using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height,
PixelFormat.Format32bppRgb))
The problem being that I'm selecting the PixelFormat, not letting it be the format of the original image. The correct code is here:
public static byte[] ResizeImageFile(byte[] imageFile, int targetSize)
{
using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
{
Size newSize = CalculateDimensions(oldImage.Size, targetSize);
using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height,
oldImage.PixelFormat))
{
newImage.SetResolution(oldImage.HorizontalResolution,
oldImage.VerticalResolution);
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();
}
}
}
}

Adding text to an image file

I need to add text to an image file. I need to read one image file (jpg,png,gif) and I need add one line text to it.
Well in GDI+ you would read in the file using a Image class and then use the Graphics class to add text to it. Something like:
Image image = Image.FromFile(#"c:\somepic.gif"); //or .jpg, etc...
Graphics graphics = Graphics.FromImage(image);
graphics.DrawString("Hello", this.Font, Brushes.Black, 0, 0);
If you want to save the file over the old one, the code has to change a bit as the Image.FromFile() method locks the file until it's disposed. The following is what I came up with:
FileStream fs = new FileStream(#"c:\somepic.gif", FileMode.Open, FileAccess.Read);
Image image = Image.FromStream(fs);
fs.Close();
Bitmap b = new Bitmap(image);
Graphics graphics = Graphics.FromImage(b);
graphics.DrawString("Hello", this.Font, Brushes.Black, 0, 0);
b.Save(#"c:\somepic.gif", image.RawFormat);
image.Dispose();
b.Dispose();
I would test this quite thoroughly though :)
Specifically for gifs to have a gif result, you should write on each frame like the following:
string originalImgPath = #"C:\test.gif";
Image IMG = Image.FromFile(originalImgPath);
FrameDimension dimension = new FrameDimension(IMG.FrameDimensionsList[0]);
int frameCount = IMG.GetFrameCount(dimension);
int Length = frameCount;
GifBitmapEncoder gEnc = new GifBitmapEncoder();
for (int i = 0; i < Length; i++)
{
// Get each frame
IMG.SelectActiveFrame(dimension, i);
var aFrame = new Bitmap(IMG);
// write one the selected frame
Graphics graphics = Graphics.FromImage(aFrame);
graphics.DrawString("Hello", new Font("Arial", 24, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, 50, 50);
var bmp = aFrame.GetHbitmap();
var src = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bmp,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
// merge frames
gEnc.Frames.Add(BitmapFrame.Create(src));
}
string saveImgFile = #"C:\modified_test.gif"
using (FileStream fs2 = new FileStream(saveImgFile, FileMode.Create))
{
gEnc.Save(fs2);
}
I should have mentioned that getting gif frames from this post.
You can do this by using the Graphics object in C#. You can get a Graphics object from the picture ( image.CreateGraphics() - or something like this as I remember ) and the use some of the built in methods for adding text to it like : Graphycs.DrawString() or other related methods.

Categories