DGL
https://delphigl.com/forum/

Screenshots
https://delphigl.com/forum/viewtopic.php?f=19&t=3185
Seite 1 von 1

Autor:  Stucuk [ Fr Aug 20, 2004 01:09 ]
Betreff des Beitrags:  Screenshots

Below is code based on Q2Delphi code for screenshots

Code:
  1.  
  2. function ScreenShot_BitmapResult : TBitmap;
  3. var
  4.   buffer: array of byte;
  5.   x,y, i: integer;
  6.   Bitmap : TBitmap;
  7.   Stream : TMemoryStream;
  8.   WW : integer;
  9. begin
  10.   {
  11.   WW forces the thing to Get an image that has an even width, even when its ment to b odd
  12.   this fixes the prob with an odd width.
  13.   }
  14.   if MainViewWidth div 2 = MainViewWidth / 2 then
  15.   WW := MainViewWidth
  16.   else
  17.   WW := MainViewWidth+1;
  18.  
  19.   SetLength(buffer, (WW * MainViewHeight * 3)+1);
  20.  
  21.   glReadPixels(0, 0, WW, MainViewHeight, GL_RGB, GL_UNSIGNED_BYTE, Pointer(Cardinal(buffer) {+ 18}));
  22.  
  23.   i := 0;
  24.   Bitmap := TBitmap.Create;
  25.   Bitmap.Canvas.Brush.Color := TVector3fToTColor(BGColor);
  26.   Bitmap.Width := MainViewWidth;
  27.   Bitmap.Height := MainViewHeight;
  28.   for y := 1 to MainViewHeight do
  29.   for x := 1 to WW do
  30.   begin
  31.   if x <= MainViewWidth then
  32.   Bitmap.Canvas.Pixels[x-1,MainViewHeight-y] := RGB(buffer[(i*3)],buffer[(i*3)+1],buffer[(i*3)+2]);
  33.   inc(i);
  34.   end;
  35.   finalize(buffer);
  36.   Result := Bitmap;
  37. end;
  38.  


The problem being at some sizes the images are not converted to the bitmap properly, results below:

Bild

EDIT: whats with the "u can't post so soon, bla bla bla" crap that happens if u spot a mistake n try n edit it in under 30 secs.

Dateianhänge:
Dateikommentar: Normal Image
_0052.jpg [140.21 KiB]
101-mal heruntergeladen

Autor:  BTierens [ Fr Aug 20, 2004 07:15 ]
Betreff des Beitrags: 

I don't see an error but why does your for loop start by 1 and not by 0?
Code:
  1.  
  2.   for y := 1 to MainViewHeight do
  3.   for x := 1 to WW do
  4.   begin
  5.   if x <= MainViewWidth then
  6.   Bitmap.Canvas.Pixels[x-1,MainViewHeight-y] := RGB(buffer[(i*3)],buffer[(i*3)+1],buffer[(i*3)+2]);
  7.   inc(i);
  8.   end;
  9.  

Autor:  rochus [ Fr Aug 20, 2004 12:29 ]
Betreff des Beitrags: 

the 1 minute "crap" is that no one floods the forum.


so far
rochus

Autor:  Stucuk [ Fr Aug 20, 2004 16:11 ]
Betreff des Beitrags: 

BTierens hat geschrieben:
I don't see an error but why does your for loop start by 1 and not by 0?
Code:
  1.  
  2.   for y := 1 to MainViewHeight do
  3.   for x := 1 to WW do
  4.   begin
  5.   if x <= MainViewWidth then
  6.   Bitmap.Canvas.Pixels[x-1,MainViewHeight-y] := RGB(buffer[(i*3)],buffer[(i*3)+1],buffer[(i*3)+2]);
  7.   inc(i);
  8.   end;
  9.  


I is the thing that is increased, it is the varible that detrmins which bit of the buffer gets turned into a colour for the canvas.pixels at any one time.

Zitat:
Bitmap.Canvas.Pixels[x-1,MainViewHeight-y] := RGB(buffer[(i*3)],buffer[(i*3)+1],buffer[(i*3)+2]);


Having the loop like

Code:
  1.  
  2. for y := 0 to MainViewHeight-1 do
  3. for x := 0 to WW-1 do
  4.  


Wouldn't change a thing.


Zitat:
the 1 minute "crap" is that no one floods the forum.


so far
rochus


Yes but u can't flood a forum by EDITING a post, which is my point. Anti Floods shouldn't ever happen when u edit posts.

Autor:  Sascha Willems [ Fr Aug 20, 2004 16:20 ]
Betreff des Beitrags: 

Don't know if it'll change something, but have you tried to explicit set the pixelformat of the bitmap, something like this :
Code:
  1. Bitmap.PixelFormat := pf24bit;

before writing pixels to it? I had to do that when I used bitmaps to create some terrain textures in an app.

Autor:  Stucuk [ Fr Aug 20, 2004 16:36 ]
Betreff des Beitrags: 

Didn't change a thing, one thing i have noticed is that even if the order of the buffer is wrongly worked out, each pixel on the image should retain its correct colours, since the buffer is always made up of 3 bytes of colour to one pixel..... yet its not keeping the correct colours.... glReadPixels is wierd (and unstable, crashed my pc months ago when i was playing with values...)

Autor:  Sascha Willems [ Fr Aug 20, 2004 19:13 ]
Betreff des Beitrags: 

Uh, I almost forgot that I (long time ago) wrote a function that takes a screenshot from the GL and stores it as a JPG. So if you don't mind the JPG format, then feel free to use my procedure in your app :

Code:
  1. // =============================================================================
  2. //  glSaveScreen
  3. // =============================================================================
  4. //  Speichert einen Screenshot des aktuellen Pufferinhaltes
  5. // =============================================================================
  6. procedure glSaveScreen(pFilename : String);
  7. var
  8.  Viewport : array[0..3] of TGLint;
  9.  JPG      : TJPEGImage;
  10.  RGBBits  : PRGBQuad;
  11.  Pixel    : PRGBQuad;
  12.  BMP      : TBitmap;
  13.  Header   : PBitmapInfo;
  14.  x,y      : Integer;
  15.  Temp     : Byte;
  16. begin
  17. glGetIntegerv(GL_VIEWPORT, @Viewport);
  18. GetMem(RGBBits, Viewport[2]*Viewport[3]*4);
  19. glFinish;
  20. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  21. glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  22. glPixelStorei(GL_PACK_SKIP_ROWS, 0);
  23. glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
  24. glReadPixels(0, 0, Viewport[2], Viewport[3], GL_RGBA, GL_UNSIGNED_BYTE, RGBBits);
  25. // Screenshot als JPG speichern
  26. JPG := TJPEGImage.Create;
  27. BMP := TBitmap.Create;
  28. BMP.PixelFormat := pf32Bit;
  29. BMP.Width       := Viewport[2];
  30. BMP.Height      := Viewport[3];
  31. GetMem(Header, SizeOf(TBitmapInfoHeader));
  32. with Header^.bmiHeader do
  33.  begin
  34.  biSize        := SizeOf(TBitmapInfoHeader);
  35.  biWidth       := Viewport[2];
  36.  biHeight      := Viewport[3];
  37.  biPlanes      := 1;
  38.  biBitCount    := 32;
  39.  biCompression := BI_RGB;
  40.  biSizeImage   := Viewport[2]*Viewport[3]*4;
  41.  end;
  42. // Rot und Blau vertauschen
  43. Pixel := RGBBits;
  44. for x := 0 to Viewport[2]-1 do
  45.  for y := 0 to Viewport[3]-1 do
  46.   begin
  47.   Temp       := Pixel.Red;
  48.   Pixel.Red  := Pixel.Blue;
  49.   Pixel.Blue := Temp;
  50.   inc(Pixel);
  51.   end;
  52. SetDIBits(Bmp.Canvas.Handle, Bmp.Handle, 0, Viewport[3], RGBBits, TBitmapInfo(Header^), DIB_RGB_COLORS);
  53. JPG.CompressionQuality := 100;
  54. JPG.Assign(BMP);
  55. JPG.SaveToFile(pFileName);
  56. FreeMem(Header);
  57. FreeMem(RGBBits);
  58. JPG.Free;
  59. BMP.Free;
  60. end;


P.S. : I've just tested it with different window sizes (odd and even) and it always worked.

Autor:  Stucuk [ Fr Aug 20, 2004 20:32 ]
Betreff des Beitrags: 

Thanks :) works perfectly (simple to convert it so it outputs a bmp as a result)

Btw did u manaualy declare PRGBQuad cos windows has the red and blue as rgbred and rgbblue.

Autor:  Sascha Willems [ Fr Aug 20, 2004 20:36 ]
Betreff des Beitrags: 

Yes, I declared it manually, sry for forgetting that part :
Code:
  1.  PRGBQuad = ^TRGBQuad;
  2.  TRGBQuad = packed record
  3.    Red   : Byte;
  4.    Green : Byte;
  5.    Blue  : Byte;
  6.    Alpha : Byte;
  7.   end;

Seite 1 von 1 Alle Zeiten sind UTC + 1 Stunde
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/