Recent

Author Topic: BitBlt on same screen got different result  (Read 3035 times)

powerpcer

  • Jr. Member
  • **
  • Posts: 55
BitBlt on same screen got different result
« on: March 15, 2021, 08:38:31 am »
when i get fullshot of desktop use this code from , and show in TImage, looks good.
Code: Pascal  [Select][+][-]
  1. FDC := GetDC(GetDesktopWindow);
  2. FBmp1.SetSize(FWidth, FHeight);
  3. cBMP1 := CreateCompatibleBitmap(FDC, FWidth, FHeight);
  4. FBmp1.Handle:=cBMP1;
  5. BitBlt(FBmp1.Canvas.Handle, 0, 0, FWidth, FHeight, FDC, 0, 0, SRCCOPY or $40000000);

but if i just want the first scanline of desktop use
Code: Pascal  [Select][+][-]
  1. FDC := GetDC(GetDesktopWindow);
  2. FBmp2.SetSize(FWidth, 1);
  3. cBMP2 := CreateCompatibleBitmap(FDC, FWidth, 1);
  4. FBmp2.Handle:=cBMP2;
  5. BitBlt(FBmp2.Canvas.Handle, 0, 0, FWidth, 1, FDC, 0, i, SRCCOPY or $40000000);
  6. FBmp2.Canvas.Changed;
  7.  
after it, FBmp1.scanline[0] is different from FBmp1.scanlien[0].
when i look into it, there are little different from R/G, but not B.
i had test this code in Win7 or Win10. got same result.
any suggestion? is that GDI+ problem?
but the same code in Old win32 program that compile with D7 runs good.

engkin

  • Hero Member
  • *****
  • Posts: 2924
Re: BitBlt on same screen got different result
« Reply #1 on: March 15, 2021, 05:12:42 pm »
Logically, if it works in Delphi it should work in Lazarus, right?

Try adding unit Windows to your uses section as the last unit. If you "attach" a sample project that would encourage lazy people, like me, to help.

powerpcer

  • Jr. Member
  • **
  • Posts: 55
Re: BitBlt on same screen got different result
« Reply #2 on: March 16, 2021, 12:19:50 pm »
ok, i make a sample, but this time, partial bitblt got fully white bitmap

engkin

  • Hero Member
  • *****
  • Posts: 2924
Re: BitBlt on same screen got different result
« Reply #3 on: March 16, 2021, 09:21:40 pm »
Looking at your code I see you are using the wrong pixel format:
TPixelFormat(getColorDepth())
or
TPixelFormat(GetDeviceCaps(h, BITSPIXEL))
is not a correct typecast GetDeviceCaps(h, BITSPIXEL) returns 1,2,4,8,15,16,32

while TPixelFormat has a range of values of 0..8:
pfDevice: 0
pf1bit: 1
pf4bit: 2
pf8bit: 3
pf15bit: 4
pf16bit: 5
pf24bit: 6
pf32bit: 7
pfCustom: 8

powerpcer

  • Jr. Member
  • **
  • Posts: 55
Re: BitBlt on same screen got different result
« Reply #4 on: March 17, 2021, 03:23:30 am »
if wrong, how can i cap the fullscreen?
that'is old code current in use, and you can change to what you think, and make a test
« Last Edit: March 17, 2021, 03:49:39 am by powerpcer »

engkin

  • Hero Member
  • *****
  • Posts: 2924
Re: BitBlt on same screen got different result
« Reply #5 on: March 17, 2021, 05:55:28 am »
Again, your SetPixelFormat is wrong, and irrelevent. When you change the handle, it gets the correct PixelFormat so that part of your code is both, wrong and useless. That's why you get the full capture.

As for the single line capture, it is correct, and yes it is a black line. Try to capture somewhere in the middle and you'll see that it works. Zoom on the full-screen capture and you'll see the same balck line.

powerpcer

  • Jr. Member
  • **
  • Posts: 55
Re: BitBlt on same screen got different result
« Reply #6 on: March 17, 2021, 06:41:01 am »
i had change the pixelformat i get the same result, fully white.
and again, i check with the full capture,i did find any fully white line.

engkin

  • Hero Member
  • *****
  • Posts: 2924
Re: BitBlt on same screen got different result
« Reply #7 on: March 17, 2021, 07:00:07 am »
What color depth do you have for you screen? can you do two tests, say, change it to 32bits and 16bits and compare the results? Also, can you capture a little more than one pixel, like 10 pixels and the 100 pixels, what do you get?

engkin

  • Hero Member
  • *****
  • Posts: 2924
Re: BitBlt on same screen got different result
« Reply #8 on: March 17, 2021, 07:03:03 am »
What do you get for PixelFormat after changing the handle:
Code: Pascal  [Select][+][-]
  1.     FBmp1.Handle := cBMP1;
  2.     FBmp1.PixelFormat;//<----- this, here

for both FBmp1 and FBmp2?

engkin

  • Hero Member
  • *****
  • Posts: 2924
Re: BitBlt on same screen got different result
« Reply #9 on: March 17, 2021, 07:15:12 am »
Do you get a white line if you capture a line in the middle?

You said now its white, what color was it before, and what did you change? On my side, it is black when I use 16bits color depth, and part of dark texture when I use 32bits.

powerpcer

  • Jr. Member
  • **
  • Posts: 55
Re: BitBlt on same screen got different result
« Reply #10 on: March 17, 2021, 08:38:32 am »
i just giveup to use bitblt for capture part of screen,
i tested.
first i use bitblt to capture fullscreen, and then if i use bitblt to capture 2nd bitmap too, if use ScanLine to compare the value of first line.
it shows that they are different, but if i savetofile, they are same.
but if i use loadfromdevice for 2nd bitmap , it shows same. also savetofile too.
wow...
and i attach what i get from my program. that looks differ from you.
and last, bitmap canvas draw get high CPU usage, when i draw a fullscreen with 1920X1080 with 30ms period.
« Last Edit: March 17, 2021, 08:57:26 am by powerpcer »

powerpcer

  • Jr. Member
  • **
  • Posts: 55
Re: BitBlt on same screen got different result
« Reply #11 on: March 19, 2021, 06:58:01 am »
making more testing on this
1. capture single line other than top most one, will get a normal line but differ to that position in fullshot.
2. font in fullshot  looks smaller.
3. cannot use it for copying full or part of bitmap. i had tested with createcompitableDC and selectobject like C
4. tbitmap.canvas.draw(0,0,other bitmap) will not copy too.
5. set a timer to 30ms for drawing a captured shot in TImage, my 6 cores Intel CPU runs at 14% usage, almost 100% of 1 core, amazing.........

all above testing did just for my old delphi vcl code port to FPC, but now, look Difficult to do.
for delphi  vcl code, item 1,2,3 can use bitblt with bitmap.canvas.lock to do.
item 5 just consume 1%

powerpcer

  • Jr. Member
  • **
  • Posts: 55
Re: BitBlt on same screen got different result
« Reply #12 on: March 21, 2021, 11:36:07 am »
after port it back to DELPHI XE VCL, server consume 11%, client consume 0.4%.
all with old style delphi code.

 

TinyPortal © 2005-2018