Jump to content

  • Log In with Google      Sign In   
  • Create Account

Taking screenshot & saving to BMP file


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Zaoshi Kaba   Crossbones+   -  Reputation: 4574

Like
0Likes
Like

Posted 08 February 2013 - 10:02 AM

Hello,

 

Firstly, I am unsure whether this problem is related to D3D11 or writing BMP file. Basically I'm trying to take screenshot of my backbuffer and write it into BMP file. However, image is extremely distorted: http://img703.imageshack.us/img703/6115/17480660.jpg. I've failed to figure what could be the problem and hope someone might help me.

 

Here's my code:

// get resolution
auto backbufferDimension = GetBackBufferDimensions();

// create staging texture
D3D11_TEXTURE2D_DESC desc;
desc.Width = backbufferDimension.x;
desc.Height = backbufferDimension.y;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_STAGING;
desc.BindFlags = 0;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
desc.MiscFlags = 0;
auto tex = CreateTexture2D(desc);

// copy backbuffer
GetContext()->CopyResource(tex, backbuffer);

// copy backbuffer into RAM
D3D11_MAPPED_SUBRESOURCE subresource;
GetContext()->Map(tex, 0, D3D11_MAP_READ, 0, &subresource);
uint32 size = backbufferDimension.y * subresource.RowPitch; // someone said row pitch MIGHT be bigger than width * 4
vector<uint8> pixelData(size);
memcpy(pixelData.data(), subresource.pData, size);
GetContext()->Unmap(tex, 0);

// some vars
const uint8 padding[] = {0, 0, 0};
const int paddingSize = (4 - (backbufferDimension.x * 3) % 4) % 4; // BMP's rowpitch must be multiple of 4 (?)
const int rowPitch = backbufferDimension.x * 3 + paddingSize;

// BMP header
BITMAPFILEHEADER bmfh = {};
bmfh.bfType = 'MB';
bmfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + rowPitch * backbufferDimension.y;
bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

// bitmap info
BITMAPINFOHEADER bmih = {};
bmih.biSize = sizeof(BITMAPINFOHEADER);
bmih.biBitCount = 24;
bmih.biWidth = (int32)backbufferDimension.x;
bmih.biHeight = -(int32)backbufferDimension.y;
bmih.biPlanes = 1;
bmih.biSizeImage = rowPitch * backbufferDimension.y;

// write data to file
fwrite(&bmfh, sizeof(bmfh), 1, file); // write BMP header
fwrite(&bmih, sizeof(bmih), 1, file); // write bitmap info
struct Pixel { uint8 r, g, b, a; };
for(uint32 y = 0; y < backbufferDimension.y; ++y) {
    uint8 *rowStart = pixelData.data() + y * subresource.RowPitch; // address where row starts according to rowpitch
    Pixel *pixels = (Pixel*)rowStart; // just a typecast from bytes into pixels
    for(uint32 x = 0; x < backbufferDimension.x; ++x) {
        Pixel &pixel = pixels[x]; // get x'th pixel in the row
        fwrite(&pixel.b, 1, 1, file); // write colors
        fwrite(&pixel.g, 1, 1, file);
        fwrite(&pixel.r, 1, 1, file);
    }
    fwrite(padding, paddingSize, 1, file); // finish row with padding
}

 

 



Sponsor:

#2 eppo   Crossbones+   -  Reputation: 2621

Like
1Likes
Like

Posted 09 February 2013 - 06:04 AM

It's likely a pitch-offset error. Instead of manually writing the contents of a BMP you're probably better of using an existing image writer. For example, a WIC (Windows Imaging Component) object can be initialized directly using Map() data and write out to a large number of different file formats.



#3 Zaoshi Kaba   Crossbones+   -  Reputation: 4574

Like
0Likes
Like

Posted 12 February 2013 - 01:14 AM

Thank you, eppo. WIC worked nicely; though it took me some time to find how to convert pixels and then figure why half of png image was missing (skybox had alpha of 0).






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS