Discussion:
How to convert image from Format1bppIndexed to Format24bppRgb?
(too old to reply)
JY
2005-04-22 19:52:45 UTC
Permalink
All of my images that are 24bppRgb are also about 41MB in size,
10000x10000 pixels are fine when I make calls to FromImage, DrawImage,
etc. It's only when I try to convert the 1bpp to 24bppRbg that I run
into the problem when I make calls to DrawImage (in the process of
trying to convert pixel formats)...

I also have 1GB RAM so I don't really think that I'm literally "Out of
memory" (how did you calculate that I would need 128MB?)

Any other ideas? My code looks ok?

Thanks again for your help!!

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
That suggests to me that it's a bit large then.. If the image was
square
it'd be 6556 pixels on a side. To make a 24 bit per pixel image you'd
require 128 megabytes of memory.

There is a possibility that the resulting image is indeed too large
for the
machine.
--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

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

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Thanks for your response Bob... The 1bpp mage is about 41MB
How big is the 1bpp image?
--
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Hi,
I'm trying to convert an image with PixelFormat Format1bppIndexed
to
Format24bppRgb. I keep getting the "Out of memory" message.
Here's what I've tried:(
(where bm.PixelFormat is Format1bppIndexed)
1)
bm2 = New Bitmap(bm.Width, bm.Height, PixelFormat.Format24bppRgb)
g2 As Graphics = Graphics.FromImage(bm2)
g2.DrawImage(bm, subRect, 0, 0, bm.Width, bm.Height,
GraphicsUnit.Pixel)
-> get "Out of memory"
2)
bm2 = bm.Clone(New Rectangle(0, 0, bm.Width, bm.Height),
PixelFormat.Format24bppRgb)
-> the new bitmap bm2 is Format1bppIndexed, not Format24bppRgb
Any ideas? I don't necessarily need the image to be
Format24bppRgb - I
just need to be able to use the function "Graphics.FromImage" (so
it
could be Format16bppRgb555, etc). Any help will be greatly
appreciated!!
Bob Powell [MVP]
2005-04-23 15:52:04 UTC
Permalink
The size of the image file has no bearing on the amount of memory you need
to represent that file internally. A JPEG image is compressed and when the
image is read and turned into a raster the memory requirements are well
known and fixed. The size of a 1bpp image is 24 times smaller than the
equivalent 24bpp image in memory.

If your file has 10,000 by 10,000 pixels that will require 3*10000*10000
bytes (300 million bytes). To store an Argb file would be 400 million bytes.

Given that your system might be using a considerable chunk of the 1 gig
available and the image might require 400 megs as well you're running fairly
close to the limits.

Paging of virtual memory is slow and not really designed for huge amounts of
memory that images require so that will further restrict the system.

You're correct in saying that the code looks fine. If the image were smaller
it'd work just fine. When the system says you're out of memory believe it.
It knows.

Add a bunch of 1 gb simms and things will be happier.
--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

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

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Post by JY
All of my images that are 24bppRgb are also about 41MB in size,
10000x10000 pixels are fine when I make calls to FromImage, DrawImage,
etc. It's only when I try to convert the 1bpp to 24bppRbg that I run
into the problem when I make calls to DrawImage (in the process of
trying to convert pixel formats)...
I also have 1GB RAM so I don't really think that I'm literally "Out of
memory" (how did you calculate that I would need 128MB?)
Any other ideas? My code looks ok?
Thanks again for your help!!
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
That suggests to me that it's a bit large then.. If the image was
square
it'd be 6556 pixels on a side. To make a 24 bit per pixel image you'd
require 128 megabytes of memory.
There is a possibility that the resulting image is indeed too large
for the
machine.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Thanks for your response Bob... The 1bpp mage is about 41MB
How big is the 1bpp image?
--
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Hi,
I'm trying to convert an image with PixelFormat Format1bppIndexed
to
Format24bppRgb. I keep getting the "Out of memory" message.
Here's what I've tried:(
(where bm.PixelFormat is Format1bppIndexed)
1)
bm2 = New Bitmap(bm.Width, bm.Height, PixelFormat.Format24bppRgb)
g2 As Graphics = Graphics.FromImage(bm2)
g2.DrawImage(bm, subRect, 0, 0, bm.Width, bm.Height,
GraphicsUnit.Pixel)
-> get "Out of memory"
2)
bm2 = bm.Clone(New Rectangle(0, 0, bm.Width, bm.Height),
PixelFormat.Format24bppRgb)
-> the new bitmap bm2 is Format1bppIndexed, not Format24bppRgb
Any ideas? I don't necessarily need the image to be
Format24bppRgb - I
just need to be able to use the function "Graphics.FromImage" (so
it
could be Format16bppRgb555, etc). Any help will be greatly
appreciated!!
b***@hotmail.com
2005-04-24 06:57:42 UTC
Permalink
Thanks Bob - one last question for you...

When I use clone() on bitmap bm, the resulting bitmap bm2 is still 1bpp
even though I've specified that I want it to be 24bpp - also a memory
problem? Since no error is generated, should I just assume that instead
of trying to create the 24bpp and generating an "out of memory" error,
the resulting bm2 just takes on the pixel format of the cloned bm?

2)
bm2 = bm.Clone(New
Rectangle(0,0,bm.Width,bm.Height),PixelFormat.Format24bppRgb)

-> the new bitmap bm2 is Format1bppIndexed, not Format24bppRgb

Thanks again for all your help!!
Post by Bob Powell [MVP]
The size of the image file has no bearing on the amount of memory you need
to represent that file internally. A JPEG image is compressed and when the
image is read and turned into a raster the memory requirements are well
known and fixed. The size of a 1bpp image is 24 times smaller than the
equivalent 24bpp image in memory.
If your file has 10,000 by 10,000 pixels that will require
3*10000*10000
Post by Bob Powell [MVP]
bytes (300 million bytes). To store an Argb file would be 400 million bytes.
Given that your system might be using a considerable chunk of the 1 gig
available and the image might require 400 megs as well you're running fairly
close to the limits.
Paging of virtual memory is slow and not really designed for huge amounts of
memory that images require so that will further restrict the system.
You're correct in saying that the code looks fine. If the image were smaller
it'd work just fine. When the system says you're out of memory
believe it.
Post by Bob Powell [MVP]
It knows.
Add a bunch of 1 gb simms and things will be happier.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Post by JY
All of my images that are 24bppRgb are also about 41MB in size,
10000x10000 pixels are fine when I make calls to FromImage,
DrawImage,
Post by Bob Powell [MVP]
Post by JY
etc. It's only when I try to convert the 1bpp to 24bppRbg that I run
into the problem when I make calls to DrawImage (in the process of
trying to convert pixel formats)...
I also have 1GB RAM so I don't really think that I'm literally "Out of
memory" (how did you calculate that I would need 128MB?)
Any other ideas? My code looks ok?
Thanks again for your help!!
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Post by Bob Powell [MVP]
Post by JY
That suggests to me that it's a bit large then.. If the image was
square
it'd be 6556 pixels on a side. To make a 24 bit per pixel image you'd
require 128 megabytes of memory.
There is a possibility that the resulting image is indeed too large
for the
machine.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Thanks for your response Bob... The 1bpp mage is about 41MB
How big is the 1bpp image?
--
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Hi,
I'm trying to convert an image with PixelFormat
Format1bppIndexed
Post by Bob Powell [MVP]
Post by JY
to
Format24bppRgb. I keep getting the "Out of memory" message.
Here's what I've tried:(
(where bm.PixelFormat is Format1bppIndexed)
1)
bm2 = New Bitmap(bm.Width, bm.Height,
PixelFormat.Format24bppRgb)
Post by Bob Powell [MVP]
Post by JY
g2 As Graphics = Graphics.FromImage(bm2)
g2.DrawImage(bm, subRect, 0, 0, bm.Width, bm.Height,
GraphicsUnit.Pixel)
-> get "Out of memory"
2)
bm2 = bm.Clone(New Rectangle(0, 0, bm.Width, bm.Height),
PixelFormat.Format24bppRgb)
-> the new bitmap bm2 is Format1bppIndexed, not Format24bppRgb
Any ideas? I don't necessarily need the image to be
Format24bppRgb - I
just need to be able to use the function "Graphics.FromImage" (so
it
could be Format16bppRgb555, etc). Any help will be greatly
appreciated!!
Bob Powell [MVP]
2005-04-24 10:08:51 UTC
Permalink
Clone is for creating a copy so it stands to reason that the clone of a 1bpp
bitmap will also be a 1bpp bitmap.

To copy a bitmap to another format you have to convert the pixels.

In the case of conversion from any type of image to a 24 or 32 bit image you
just need to create a bitmap of the correct size, obtain a Graphics object
for the new image and draw the old image onto it using DrawImage. In the
case of conversion to any indexed type you need to understand the pixel
format, lock the new image using lockbits and convert the data pixel by
pixel.
--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

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

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Post by b***@hotmail.com
Thanks Bob - one last question for you...
When I use clone() on bitmap bm, the resulting bitmap bm2 is still 1bpp
even though I've specified that I want it to be 24bpp - also a memory
problem? Since no error is generated, should I just assume that instead
of trying to create the 24bpp and generating an "out of memory" error,
the resulting bm2 just takes on the pixel format of the cloned bm?
2)
bm2 = bm.Clone(New
Rectangle(0,0,bm.Width,bm.Height),PixelFormat.Format24bppRgb)
-> the new bitmap bm2 is Format1bppIndexed, not Format24bppRgb
Thanks again for all your help!!
Post by Bob Powell [MVP]
The size of the image file has no bearing on the amount of memory you
need
Post by Bob Powell [MVP]
to represent that file internally. A JPEG image is compressed and
when the
Post by Bob Powell [MVP]
image is read and turned into a raster the memory requirements are
well
Post by Bob Powell [MVP]
known and fixed. The size of a 1bpp image is 24 times smaller than
the
Post by Bob Powell [MVP]
equivalent 24bpp image in memory.
If your file has 10,000 by 10,000 pixels that will require
3*10000*10000
Post by Bob Powell [MVP]
bytes (300 million bytes). To store an Argb file would be 400 million
bytes.
Post by Bob Powell [MVP]
Given that your system might be using a considerable chunk of the 1
gig
Post by Bob Powell [MVP]
available and the image might require 400 megs as well you're running
fairly
Post by Bob Powell [MVP]
close to the limits.
Paging of virtual memory is slow and not really designed for huge
amounts of
Post by Bob Powell [MVP]
memory that images require so that will further restrict the system.
You're correct in saying that the code looks fine. If the image were
smaller
Post by Bob Powell [MVP]
it'd work just fine. When the system says you're out of memory
believe it.
Post by Bob Powell [MVP]
It knows.
Add a bunch of 1 gb simms and things will be happier.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Post by JY
All of my images that are 24bppRgb are also about 41MB in size,
10000x10000 pixels are fine when I make calls to FromImage,
DrawImage,
Post by Bob Powell [MVP]
Post by JY
etc. It's only when I try to convert the 1bpp to 24bppRbg that I
run
Post by Bob Powell [MVP]
Post by JY
into the problem when I make calls to DrawImage (in the process of
trying to convert pixel formats)...
I also have 1GB RAM so I don't really think that I'm literally "Out
of
Post by Bob Powell [MVP]
Post by JY
memory" (how did you calculate that I would need 128MB?)
Any other ideas? My code looks ok?
Thanks again for your help!!
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Post by Bob Powell [MVP]
Post by JY
That suggests to me that it's a bit large then.. If the image was
square
it'd be 6556 pixels on a side. To make a 24 bit per pixel image
you'd
Post by Bob Powell [MVP]
Post by JY
require 128 megabytes of memory.
There is a possibility that the resulting image is indeed too large
for the
machine.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Thanks for your response Bob... The 1bpp mage is about 41MB
How big is the 1bpp image?
--
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and
Tricks
Post by Bob Powell [MVP]
Post by JY
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Hi,
I'm trying to convert an image with PixelFormat
Format1bppIndexed
Post by Bob Powell [MVP]
Post by JY
to
Format24bppRgb. I keep getting the "Out of memory" message.
Here's what I've tried:(
(where bm.PixelFormat is Format1bppIndexed)
1)
bm2 = New Bitmap(bm.Width, bm.Height,
PixelFormat.Format24bppRgb)
Post by Bob Powell [MVP]
Post by JY
g2 As Graphics = Graphics.FromImage(bm2)
g2.DrawImage(bm, subRect, 0, 0, bm.Width, bm.Height,
GraphicsUnit.Pixel)
-> get "Out of memory"
2)
bm2 = bm.Clone(New Rectangle(0, 0, bm.Width, bm.Height),
PixelFormat.Format24bppRgb)
-> the new bitmap bm2 is Format1bppIndexed, not Format24bppRgb
Any ideas? I don't necessarily need the image to be
Format24bppRgb - I
just need to be able to use the function "Graphics.FromImage"
(so
Post by Bob Powell [MVP]
Post by JY
it
could be Format16bppRgb555, etc). Any help will be greatly
appreciated!!
JY
2005-04-25 16:02:36 UTC
Permalink
Maybe I'm completely missing your point (sorry if I am), but aren't I
doing exactly what you're stating I should do?

//In the case of conversion from any type of image to a 24 or 32 bit
//image you just need to create a bitmap of the correct size

Dim oRaster As Image = Image.FromFile(sRasterPath)
Dim bm As Bitmap = New Bitmap(oRaster.Width, oRaster.Height,
PixelFormat.Format24bppRgb)

//obtain a Graphics object for the new image and draw the old image
//onto it using DrawImage

Dim g As Graphics = Graphics.FromImage(bm)
Dim rect As Rectangle = New Rectangle(0, 0, bm.Width, bm.Height)
g.DrawImage(bm, rect, 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel)

Or are you saying that this SHOULD work in theory, but since my images
are so large - it's just not going to work for me? Thanks a lot Bob, I
really appreciate you taking the time to answer questions from a very
confused person! :)
Post by Bob Powell [MVP]
Clone is for creating a copy so it stands to reason that the clone of a 1bpp
bitmap will also be a 1bpp bitmap.
To copy a bitmap to another format you have to convert the pixels.
In the case of conversion from any type of image to a 24 or 32 bit image you
just need to create a bitmap of the correct size, obtain a Graphics object
for the new image and draw the old image onto it using DrawImage. In the
case of conversion to any indexed type you need to understand the pixel
format, lock the new image using lockbits and convert the data pixel by
pixel.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Post by b***@hotmail.com
Thanks Bob - one last question for you...
When I use clone() on bitmap bm, the resulting bitmap bm2 is still 1bpp
even though I've specified that I want it to be 24bpp - also a memory
problem? Since no error is generated, should I just assume that instead
of trying to create the 24bpp and generating an "out of memory" error,
the resulting bm2 just takes on the pixel format of the cloned bm?
2)
bm2 = bm.Clone(New
Rectangle(0,0,bm.Width,bm.Height),PixelFormat.Format24bppRgb)
-> the new bitmap bm2 is Format1bppIndexed, not Format24bppRgb
Thanks again for all your help!!
Post by Bob Powell [MVP]
The size of the image file has no bearing on the amount of memory you
need
Post by Bob Powell [MVP]
to represent that file internally. A JPEG image is compressed and
when the
Post by Bob Powell [MVP]
image is read and turned into a raster the memory requirements are
well
Post by Bob Powell [MVP]
known and fixed. The size of a 1bpp image is 24 times smaller than
the
Post by Bob Powell [MVP]
equivalent 24bpp image in memory.
If your file has 10,000 by 10,000 pixels that will require
3*10000*10000
Post by Bob Powell [MVP]
bytes (300 million bytes). To store an Argb file would be 400 million
bytes.
Post by Bob Powell [MVP]
Given that your system might be using a considerable chunk of the 1
gig
Post by Bob Powell [MVP]
available and the image might require 400 megs as well you're running
fairly
Post by Bob Powell [MVP]
close to the limits.
Paging of virtual memory is slow and not really designed for huge
amounts of
Post by Bob Powell [MVP]
memory that images require so that will further restrict the system.
You're correct in saying that the code looks fine. If the image were
smaller
Post by Bob Powell [MVP]
it'd work just fine. When the system says you're out of memory
believe it.
Post by Bob Powell [MVP]
It knows.
Add a bunch of 1 gb simms and things will be happier.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Post by JY
All of my images that are 24bppRgb are also about 41MB in size,
10000x10000 pixels are fine when I make calls to FromImage,
DrawImage,
Post by Bob Powell [MVP]
Post by JY
etc. It's only when I try to convert the 1bpp to 24bppRbg that I
run
Post by Bob Powell [MVP]
Post by JY
into the problem when I make calls to DrawImage (in the process of
trying to convert pixel formats)...
I also have 1GB RAM so I don't really think that I'm literally "Out
of
Post by Bob Powell [MVP]
Post by JY
memory" (how did you calculate that I would need 128MB?)
Any other ideas? My code looks ok?
Thanks again for your help!!
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Post by Bob Powell [MVP]
Post by JY
That suggests to me that it's a bit large then.. If the image was
square
it'd be 6556 pixels on a side. To make a 24 bit per pixel image
you'd
Post by Bob Powell [MVP]
Post by JY
require 128 megabytes of memory.
There is a possibility that the resulting image is indeed too large
for the
machine.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Thanks for your response Bob... The 1bpp mage is about 41MB
How big is the 1bpp image?
--
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and
Tricks
Post by Bob Powell [MVP]
Post by JY
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Hi,
I'm trying to convert an image with PixelFormat
Format1bppIndexed
Post by Bob Powell [MVP]
Post by JY
to
Format24bppRgb. I keep getting the "Out of memory" message.
Here's what I've tried:(
(where bm.PixelFormat is Format1bppIndexed)
1)
bm2 = New Bitmap(bm.Width, bm.Height,
PixelFormat.Format24bppRgb)
Post by Bob Powell [MVP]
Post by JY
g2 As Graphics = Graphics.FromImage(bm2)
g2.DrawImage(bm, subRect, 0, 0, bm.Width, bm.Height,
GraphicsUnit.Pixel)
-> get "Out of memory"
2)
bm2 = bm.Clone(New Rectangle(0, 0, bm.Width, bm.Height),
PixelFormat.Format24bppRgb)
-> the new bitmap bm2 is Format1bppIndexed, not Format24bppRgb
Any ideas? I don't necessarily need the image to be
Format24bppRgb - I
just need to be able to use the function "Graphics.FromImage"
(so
Post by Bob Powell [MVP]
Post by JY
it
could be Format16bppRgb555, etc). Any help will be greatly
appreciated!!
Bob Powell [MVP]
2005-04-25 18:15:47 UTC
Permalink
Yes, it *should* work but it doesn't because the images are too large.
--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

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

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Post by JY
Maybe I'm completely missing your point (sorry if I am), but aren't I
doing exactly what you're stating I should do?
//In the case of conversion from any type of image to a 24 or 32 bit
//image you just need to create a bitmap of the correct size
Dim oRaster As Image = Image.FromFile(sRasterPath)
Dim bm As Bitmap = New Bitmap(oRaster.Width, oRaster.Height,
PixelFormat.Format24bppRgb)
//obtain a Graphics object for the new image and draw the old image
//onto it using DrawImage
Dim g As Graphics = Graphics.FromImage(bm)
Dim rect As Rectangle = New Rectangle(0, 0, bm.Width, bm.Height)
g.DrawImage(bm, rect, 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel)
Or are you saying that this SHOULD work in theory, but since my images
are so large - it's just not going to work for me? Thanks a lot Bob, I
really appreciate you taking the time to answer questions from a very
confused person! :)
Post by Bob Powell [MVP]
Clone is for creating a copy so it stands to reason that the clone of a 1bpp
bitmap will also be a 1bpp bitmap.
To copy a bitmap to another format you have to convert the pixels.
In the case of conversion from any type of image to a 24 or 32 bit image you
just need to create a bitmap of the correct size, obtain a Graphics object
for the new image and draw the old image onto it using DrawImage. In the
case of conversion to any indexed type you need to understand the pixel
format, lock the new image using lockbits and convert the data pixel by
pixel.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Post by b***@hotmail.com
Thanks Bob - one last question for you...
When I use clone() on bitmap bm, the resulting bitmap bm2 is still 1bpp
even though I've specified that I want it to be 24bpp - also a memory
problem? Since no error is generated, should I just assume that instead
of trying to create the 24bpp and generating an "out of memory" error,
the resulting bm2 just takes on the pixel format of the cloned bm?
2)
bm2 = bm.Clone(New
Rectangle(0,0,bm.Width,bm.Height),PixelFormat.Format24bppRgb)
-> the new bitmap bm2 is Format1bppIndexed, not Format24bppRgb
Thanks again for all your help!!
Post by Bob Powell [MVP]
The size of the image file has no bearing on the amount of memory you
need
Post by Bob Powell [MVP]
to represent that file internally. A JPEG image is compressed and
when the
Post by Bob Powell [MVP]
image is read and turned into a raster the memory requirements are
well
Post by Bob Powell [MVP]
known and fixed. The size of a 1bpp image is 24 times smaller than
the
Post by Bob Powell [MVP]
equivalent 24bpp image in memory.
If your file has 10,000 by 10,000 pixels that will require
3*10000*10000
Post by Bob Powell [MVP]
bytes (300 million bytes). To store an Argb file would be 400 million
bytes.
Post by Bob Powell [MVP]
Given that your system might be using a considerable chunk of the 1
gig
Post by Bob Powell [MVP]
available and the image might require 400 megs as well you're running
fairly
Post by Bob Powell [MVP]
close to the limits.
Paging of virtual memory is slow and not really designed for huge
amounts of
Post by Bob Powell [MVP]
memory that images require so that will further restrict the system.
You're correct in saying that the code looks fine. If the image were
smaller
Post by Bob Powell [MVP]
it'd work just fine. When the system says you're out of memory
believe it.
Post by Bob Powell [MVP]
It knows.
Add a bunch of 1 gb simms and things will be happier.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Post by JY
All of my images that are 24bppRgb are also about 41MB in size,
10000x10000 pixels are fine when I make calls to FromImage,
DrawImage,
Post by Bob Powell [MVP]
Post by JY
etc. It's only when I try to convert the 1bpp to 24bppRbg that I
run
Post by Bob Powell [MVP]
Post by JY
into the problem when I make calls to DrawImage (in the process of
trying to convert pixel formats)...
I also have 1GB RAM so I don't really think that I'm literally "Out
of
Post by Bob Powell [MVP]
Post by JY
memory" (how did you calculate that I would need 128MB?)
Any other ideas? My code looks ok?
Thanks again for your help!!
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Post by Bob Powell [MVP]
Post by JY
That suggests to me that it's a bit large then.. If the image was
square
it'd be 6556 pixels on a side. To make a 24 bit per pixel image
you'd
Post by Bob Powell [MVP]
Post by JY
require 128 megabytes of memory.
There is a possibility that the resulting image is indeed too large
for the
machine.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Thanks for your response Bob... The 1bpp mage is about 41MB
How big is the 1bpp image?
--
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and
Tricks
Post by Bob Powell [MVP]
Post by JY
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Hi,
I'm trying to convert an image with PixelFormat
Format1bppIndexed
Post by Bob Powell [MVP]
Post by JY
to
Format24bppRgb. I keep getting the "Out of memory" message.
Here's what I've tried:(
(where bm.PixelFormat is Format1bppIndexed)
1)
bm2 = New Bitmap(bm.Width, bm.Height,
PixelFormat.Format24bppRgb)
Post by Bob Powell [MVP]
Post by JY
g2 As Graphics = Graphics.FromImage(bm2)
g2.DrawImage(bm, subRect, 0, 0, bm.Width, bm.Height,
GraphicsUnit.Pixel)
-> get "Out of memory"
2)
bm2 = bm.Clone(New Rectangle(0, 0, bm.Width, bm.Height),
PixelFormat.Format24bppRgb)
-> the new bitmap bm2 is Format1bppIndexed, not Format24bppRgb
Any ideas? I don't necessarily need the image to be
Format24bppRgb - I
just need to be able to use the function "Graphics.FromImage"
(so
Post by Bob Powell [MVP]
Post by JY
it
could be Format16bppRgb555, etc). Any help will be greatly
appreciated!!
Loading...