Jump to content
  • Advertisement
Sign in to follow this  
compro21

Surface to Bitmap

This topic is 2901 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all,
I'm trying to copy a Direct3D surface into a bitmap in memory, but not having much luck.

Right now, the only way that works is to do this:


GraphicsStream stream = SurfaceLoader.SaveToStream(ImageFileFormat.Bmp, device.GetRenderTarget(0));
SurfaceLoader.Save(@"~temp", ImageFileFormat.Png, device.GetRenderTarget(0));
Bitmap bmp = new Bitmap(@"~temp");

....
System.IO.File.Delete(@"~temp");



I'm trying to find a way to cut out the middleman. I've tried doing something like this:


GraphicsStream stream = SurfaceLoader.SaveToStream(ImageFileFormat.Bmp, device.GetRenderTarget(0));
Bitmap bmp = new Bitmap(size.Width, size.Height, size.Width*4, PixelFormat.Format32bppRgb, stream.InternalData);



But when I save the image out, the RGB channels are separate (that is there is a red pixel, then a green, then a blue), and the image is upside down. When I change the PixelFormat to something else, it gives me a runtime error. When I change the stride, the image just won't line up.

Any ideas on what I'm doing wrong? Anyone know how this works?

I could really use your help.

Using C# and DirectX 9.0

Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Here's the solution I came up with:


private Bitmap fromStream(Size size)
{
GraphicsStream stream = SurfaceLoader.SaveToStream(ImageFileFormat.Bmp, device.GetRenderTarget(0));
long offset = stream.Length - size.Width * size.Height * 4 - 3;
for (int i = 0; i < offset; i++)
stream.ReadByte();

Bitmap bmp = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
int x = 0, y = size.Height - 1;
for (int i = (int)offset; i < stream.Length - 3; i += 4)
{
int A = stream.ReadByte();
int B = stream.ReadByte();
int G = stream.ReadByte();
int R = stream.ReadByte();

bmp.SetPixel(x, y, Color.FromArgb(R, G, B));

x++;
if (x == size.Width)
{
x = 0;
y--;
}
}

return bmp;

}






[Edited by - compro21 on July 9, 2010 2:17:07 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!