Huge image resize

  • Thread starter Thread starter Tamir Khason
  • Start date Start date
T

Tamir Khason

I have huge bitmap inmemory (about 4K x 6K 300dpi) and I want to quick
resize it so old fasion way:
Bitmap result = new Bitmap( nSize.Width, nSize.Height);
using(
Graphics g1 = Graphics.FromImage( (Image) result ) )
{
g1.DrawImage( b, 0, 0, nSize.Width, nSize.Height );
}

working rather fine the first (and even second time), but each time it used
the process takes x2 time (no difference between make the bitmap bigger or
smaller)
so the fifth time application crashed with no memory

Anyone can advice WHY and maybe better way to do this. I have concurrent one
image in application.

I do not want to go unmanaged this time.

thx
 
Tamir,

Are you disposing of the Bitmap image correctly? You should be calling
Dispose on the images when done. Images of that size take quite a bit of
memory, and not disposing of them could cause performance degredation pretty
quickly.

Also, are you using them just as-needed, or caching them in memory? You
might want to reconsider caching them in memory if this is the case.

Hope this helps.
 
Hi, Nicholas and thank you for response,
as you can see I'm using following method for disposing:
using(
Graphics g1 = Graphics.FromImage( (Image) result ) )
because if I'll dispose the graphics before return the resized image will be
lost.
How can I cache the image within C#? are there any methods you know to do
this?

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Nicholas Paldino said:
Tamir,

Are you disposing of the Bitmap image correctly? You should be calling
Dispose on the images when done. Images of that size take quite a bit of
memory, and not disposing of them could cause performance degredation
pretty quickly.

Also, are you using them just as-needed, or caching them in memory?
You might want to reconsider caching them in memory if this is the case.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tamir Khason said:
I have huge bitmap inmemory (about 4K x 6K 300dpi) and I want to quick
resize it so old fasion way:
Bitmap result = new Bitmap( nSize.Width, nSize.Height);
using(
Graphics g1 = Graphics.FromImage( (Image) result ) )
{
g1.DrawImage( b, 0, 0, nSize.Width, nSize.Height );
}

working rather fine the first (and even second time), but each time it
used the process takes x2 time (no difference between make the bitmap
bigger or smaller)
so the fifth time application crashed with no memory

Anyone can advice WHY and maybe better way to do this. I have concurrent
one image in application.

I do not want to go unmanaged this time.

thx
 
Tamir,

You are disposing of the Graphics context, but you are not disposing of
the image. When you are done with it, are you disposing of it? How are you
using it (the Bitmap variable).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tamir Khason said:
Hi, Nicholas and thank you for response,
as you can see I'm using following method for disposing:
using(
Graphics g1 = Graphics.FromImage( (Image) result ) )
because if I'll dispose the graphics before return the resized image will
be lost.
How can I cache the image within C#? are there any methods you know to do
this?

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Nicholas Paldino said:
Tamir,

Are you disposing of the Bitmap image correctly? You should be
calling Dispose on the images when done. Images of that size take quite
a bit of memory, and not disposing of them could cause performance
degredation pretty quickly.

Also, are you using them just as-needed, or caching them in memory?
You might want to reconsider caching them in memory if this is the case.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tamir Khason said:
I have huge bitmap inmemory (about 4K x 6K 300dpi) and I want to quick
resize it so old fasion way:
Bitmap result = new Bitmap( nSize.Width, nSize.Height);
using(
Graphics g1 = Graphics.FromImage( (Image) result ) )
{
g1.DrawImage( b, 0, 0, nSize.Width, nSize.Height );
}

working rather fine the first (and even second time), but each time it
used the process takes x2 time (no difference between make the bitmap
bigger or smaller)
so the fifth time application crashed with no memory

Anyone can advice WHY and maybe better way to do this. I have concurrent
one image in application.

I do not want to go unmanaged this time.

thx
 
Bitmap m_bitmap;
//Loaded
m_bitmap = ResizeBitmap(m_bitmap,myNewSize);
it should be reused, no?


Nicholas Paldino said:
Tamir,

You are disposing of the Graphics context, but you are not disposing of
the image. When you are done with it, are you disposing of it? How are
you using it (the Bitmap variable).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tamir Khason said:
Hi, Nicholas and thank you for response,
as you can see I'm using following method for disposing:
using(
Graphics g1 = Graphics.FromImage( (Image) result ) )
because if I'll dispose the graphics before return the resized image will
be lost.
How can I cache the image within C#? are there any methods you know to do
this?

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

Nicholas Paldino said:
Tamir,

Are you disposing of the Bitmap image correctly? You should be
calling Dispose on the images when done. Images of that size take quite
a bit of memory, and not disposing of them could cause performance
degredation pretty quickly.

Also, are you using them just as-needed, or caching them in memory?
You might want to reconsider caching them in memory if this is the case.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have huge bitmap inmemory (about 4K x 6K 300dpi) and I want to quick
resize it so old fasion way:
Bitmap result = new Bitmap( nSize.Width, nSize.Height);
using(
Graphics g1 = Graphics.FromImage( (Image) result ) )
{
g1.DrawImage( b, 0, 0, nSize.Width, nSize.Height );
}

working rather fine the first (and even second time), but each time it
used the process takes x2 time (no difference between make the bitmap
bigger or smaller)
so the fifth time application crashed with no memory

Anyone can advice WHY and maybe better way to do this. I have
concurrent one image in application.

I do not want to go unmanaged this time.

thx
 
Tamir,

I'm going to assumpe that m_bitmap is a field in a class. Does this
class implement IDisposable? If so, is it being called correctly when done?
The bitmap is never being GCed as soon as it can be if it is not being
disposed (not to mention the unmanaged resources that are waiting to be
disposed of).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tamir Khason said:
Bitmap m_bitmap;
//Loaded
m_bitmap = ResizeBitmap(m_bitmap,myNewSize);
it should be reused, no?


Nicholas Paldino said:
Tamir,

You are disposing of the Graphics context, but you are not disposing
of the image. When you are done with it, are you disposing of it? How
are you using it (the Bitmap variable).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tamir Khason said:
Hi, Nicholas and thank you for response,
as you can see I'm using following method for disposing:
using(
Graphics g1 = Graphics.FromImage( (Image) result ) )
because if I'll dispose the graphics before return the resized image
will be lost.
How can I cache the image within C#? are there any methods you know to
do this?

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "

in message Tamir,

Are you disposing of the Bitmap image correctly? You should be
calling Dispose on the images when done. Images of that size take
quite a bit of memory, and not disposing of them could cause
performance degredation pretty quickly.

Also, are you using them just as-needed, or caching them in memory?
You might want to reconsider caching them in memory if this is the
case.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have huge bitmap inmemory (about 4K x 6K 300dpi) and I want to quick
resize it so old fasion way:
Bitmap result = new Bitmap( nSize.Width, nSize.Height);
using(
Graphics g1 = Graphics.FromImage( (Image) result ) )
{
g1.DrawImage( b, 0, 0, nSize.Width, nSize.Height );
}

working rather fine the first (and even second time), but each time it
used the process takes x2 time (no difference between make the bitmap
bigger or smaller)
so the fifth time application crashed with no memory

Anyone can advice WHY and maybe better way to do this. I have
concurrent one image in application.

I do not want to go unmanaged this time.

thx
 
Back
Top