Multimobile Development: Building Applications for any Smartphone
Bitmap.MakeTransparent OutOfMemoryException
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Jax
GOOD ANSWER
Whenever I use this method I get out of memory exception, even when using tiny bitmaps (100X100).
Does anyone know why this is?
From what I remember OutOfMemory Exception is a default for other exceptions, does anyone know what it might be?
I am of course using try catch and finally blocks to ensure in cases of exceptions resources get released, I just can't understand why i'm always getting this exception.

Help me people of the newsgroups, you're my only hope.

jax
Reply to this message...
Vote that this is a GOOD answer... (3 votes from other users already)
 
 
    
Bob Powell [MVP] (VIP)
GOOD ANSWER
Can you post code that demonstrates this? It seems to be working fine for
me.

--
Bob Powell [MVP]
C#, System.Drawing

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

"Jax" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
> Whenever I use this method I get out of memory exception, even when using
tiny bitmaps (100X100).
[Original message clipped]

getting this exception.
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Jax
GOOD ANSWER
It's as simple as:

(psuedo)
FileStream fs = new FileStream(@"c:\myImage.bmp", FileMode.Open);
Bitmap b = Bitmap.FromStream(fs);
try
{
Label theColour= new Label();
// client side script where the user clicks the image
int x = Convert.ToInt32(TextBox1.Text);
int y = Convert.ToInt32(TextBox2.Text);
theColour.BackColour = b.GetPixel(x,y);
b.MakeTransparent(theColour.BackColor); // Error here, OutOfMemoryException
b.SaveAs(fs, ImageFormat.Jpeg); // Occasionally error here, unspecified.
Image1.Url = "c:\myImage.Jpg";
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
b.Dispose();
fs.Close();
}

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
John Fisher
GOOD ANSWER
Hi,
I was just hunting down an answer to the same problem. It turns out that the Bitmap files I was loading were created with a 256 color bit-depth. My windows system is running 32-bit color. So, I loaded the bitmap file, then created a new Bitmap of the same width and height, but using a Graphics instance created from the desktop window. This produced a bitmap with the same color depth as my system. After that, I copied the original bitmap into the new one (with the right color depth). The new bitmap allows me to successfully call MakeTransparent!!

Here's my code.

    // Get the current desktop screen graphics.
    Graphics g = Graphics.FromHwnd(IntPtr.Zero);
    // Make a bitmap using that color depth.
    Bitmap bitmap = new Bitmap(originalbitmap.Width, originalbitmap.Height, g);
    // Clean up.
    g.Dispose();
    
    // Start drawing on our new bitmap.
    g = Graphics.FromImage(bitmap);
    // Copy the loaded bitmap to the bitmap with proper screen depth.
    g.DrawImage(originalbitmap, 0, 0, bitmap.Width, bitmap.Height);
    // Clean up.
    g.Dispose();

    // Now we can successfully make the bitmap transparent!
    bitmap.MakeTransparent(bitmap.GetPixel(0, 0));

--------------------------------
From: John Fisher
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
 
System.Convert
System.Drawing.Bitmap
System.Drawing.Graphics
System.Drawing.Imaging.ImageFormat
System.IntPtr
System.IO.FileMode
System.IO.FileStream
System.OutOfMemoryException
System.Reflection.Emit.Label
System.Web.UI.MobileControls.Label
System.Web.UI.WebControls.Label
System.Windows.Forms.Label




Multimobile Development: Building Applications for any Smartphone
Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734