PDA

View Full Version : Screenshot direct to System::Drawing::Bitmap^ variable? (Visual Studio, Managed C++)



educota
01-28-2016, 05:23 AM
Hey guys,

I have been searching for how to make screenshot function... Is there a way to store the image directly on a variable like this: System:: Drawing::Bitmap^ bmp = gcnew System:: Drawing::Bitmap(width, height);


Thank you very much!

__bob__
01-28-2016, 07:56 AM
With C++, il read the screen data like this :

unsigned char* image = (unsigned char*)malloc(sizeof(unsigned char) * 3 * with * height);
glReadPixels(0, 0, this->w_Width, this->w_Height, GL_RGB, GL_UNSIGNED_BYTE, image);

all the datas a re in image, then, the simplest (but no efficient ways) is to write this in a PPM files (big files but easy format to write):

void PPMWriter(unsigned char *in,char *name,int dimx, int dimy)
{

int i, j;
FILE *fp = fopen(name, "wb"); /* b - binary mode */
(void) fprintf(fp, "P6\n%d %d\n255\n", dimx, dimy);
for (j = 0; j < dimy; ++j)
{
for (i = 0; i < dimx; ++i)
{
static unsigned char color[3];
color[0] = in[3*i+3*j*dimy]; /* red */
color[1] = in[3*i+3*j*dimy+1]; /* green */
color[2] = in[3*i+3*j*dimy+2]; /* blue */
(void) fwrite(color, 1, 3, fp);
}
}
(void) fclose(fp);
}

I'dont know if MS C++ have specific function to vrite bmp or another format...

educota
02-01-2016, 10:11 AM
Thank you, bob.

I got it with the help of the SDL library...

With this code below, I get the screenshot saved on a bmp file


int screen_width = 1024;
int screen_height = 620;


SDL_Surface * temp = SDL_CreateRGBSurface(SDL_SWSURFACE, screen_width, screen_height, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);

char * pixels = new char[3 * screen_width * screen_height];

glReadPixels(0, 0, screen_width, screen_height, GL_RGB, GL_UNSIGNED_BYTE, pixels);

for (int i = 0; i < screen_height; i++)
std::memcpy(((char *)temp->pixels) + temp->pitch * i, pixels + 3 * screen_width * (screen_height - i - 1), screen_width * 3);

delete[] pixels;

SDL_SaveBMP(temp, "pic.bmp");

SDL_FreeSurface(temp);

And with this code below, I get the bmp file on my pictureBox when I click a button as I wanted...


private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {




pictureBox2->Image = Image::FromFile("pic.bmp");
pictureBox2->SizeMode = PictureBoxSizeMode::StretchImage;
this->pictureBox2->Visible = true;







}