Home > Back-end >  About the bitmap and Canvas replication failure problem
About the bitmap and Canvas replication failure problem

Time:10-02

Hello everyone, I am now doing a program that need to copy a part of the already created good btimap to another bitmap, and save as JPG file images, but in the process of user actual use, duplicate failure will have a chance, which preserved the JPG is white picture phenomenon, I tested the code, found that the risk is very big, use bitblt also occur, both bitmap and Canvas will appear this kind of phenomenon:
-- -- -- -- -- -- -- -- -- -- -- -- code -- -- -- -- -- -- -- -- -- -- -- --
Tempbit:=tbitmap. Create;
Tempbit. Width:=332;
Tempbit. Height:=212;
Bmp2Jpg (sbeijing, GetMySelfPath + 'photo \ temp \ temp. JPG ");
,0,332,212 tempbit. Canvas. CopyRect (the Rect (0), sbeijing. The Canvas, the Rect (0,0,332,212));
tempbit1:=TBitmap. Create;
Tempbit1. Width:=sbeijing. Width;
Tempbit1. Height:=sbeijing. Height;
Tempbit1. Canvas. CopyRec (tempbit1. Canvas. ClipRect, sbeijing. Canvas, sbeijing. Canvas. The ClipRect);
Tempbit1. SaveToFile (GetMySelfPath + 'photo \ temp \ temp1. BMP');
Tempbit. SaveToFile (GetMySelfPath + 'photo \ temp \ temp. BMP');

Bmp2Jpg (tempbit PhotoRoot);
BitBlt (tempbit. Handle, 0, 0, tempbit. Width, tempbit. Height, sbeijing. Handle, 0, 0, SRCCOPY);
Tempbit. SaveToFile (GetMySelfPath + 'photo \ temp \ temp2. BMP');

TEMPBIT. Free;
-- -- -- -- -- -- -- -- -- -- -- -- code -- -- -- -- -- -- -- -- -- -- -- --
Code above the red part of the increase test for me, want to have a look at the abnormal, various bitmap is generated correctly, found an exception occurs sbeijing keep temp. BMP can generate normal, other save pictures are all blank,
I suspect the copyrect when abnormal happens, but I used to do C/C + + is also used when bitblt didn't happen this kind of phenomenon, the photo copy frequency is very high, also does not have this kind of phenomenon, Delphi is new to the development of language, especially where there is a problem? Pixel format or other?

CodePudding user response:

Look at your code, the first confirmed sbeijing. Bitmap images?
Sbeijing is what east east?
The Bmp2Jpg (sbeijing, GetMySelfPath + 'photo \ temp \ temp. JPG "); What did the concrete execution?
Tempbit:=tbitmap. Create;
Tempbit. Width:=332;
Tempbit. Height:=212;
Bmp2Jpg (sbeijing, GetMySelfPath + 'photo \ temp \ temp. JPG ");
,0,332,212 tempbit. Canvas. CopyRect (the Rect (0), sbeijing. The Canvas, the Rect (0,0,332,212));

CodePudding user response:

The
reference 1/f, bihai response:
look at your code, first confirmed sbeijing. Bitmap images?
Sbeijing is what east east?
The Bmp2Jpg (sbeijing, GetMySelfPath + 'photo \ temp \ temp. JPG "); What did the concrete execution?
Tempbit:=tbitmap. Create;
Tempbit. Width:=332;
Tempbit. Height:=212;
Bmp2Jpg (sbeijing, GetMySelfPath + 'photo \ temp \ temp. JPG ");
,0,332,212 tempbit. Canvas. CopyRect (the Rect (0), sbeijing. The Canvas, the Rect (0,0,332,212));

Hello, thanks for your reply, I add so many red source is to test whether the image has been correctly generated, sbeijing is a programmer with a variable name, is a large bitmap, this code is mainly want from sbeijing capture part of the image and then stored inside, bmp2jpg is to use existing BMP images generated JPG format,
The question now is, sbeijing has generated correctly, cutting can be correctly generated image file (BMP), but cannot copy it properly after using the copyrect images, so the problem appeared in the copyrect function,

CodePudding user response:

Sbeijing TImage or TBitmap?
If it is a TIMAGE, sbeijing image in BMP or JPG?
JPG format images in image directly or move Canvas. The CopyRect are not enough, need to convert,

The following code will not appear the phenomenon of blank:
Procedure TForm1. Button1Click (Sender: TObject);
The begin
Image1. Picture. LoadFromFile (Edit1. Text);
Image2. Picture. Bitmap. Assign (Image1. Picture. Graphic);
Image3. Picture. Bitmap. Width:=332;
Image3. Picture. Bitmap. Height:=212;
Image3. Picture. Bitmap. Canvas. CopyRect (the Rect (0,0,332,212), Image2. The Picture. The Bitmap. The Canvas, the Rect (0,0,332,212));
end;

CodePudding user response:

The
reference bihai reply: 3/f
sbeijing TImage or TBitmap?
If it is a TIMAGE, sbeijing image in BMP or JPG?
JPG format images in image directly or move Canvas. The CopyRect are not enough, need to convert,

The following code will not appear the phenomenon of blank:
Procedure TForm1. Button1Click (Sender: TObject);
The begin
Image1. Picture. LoadFromFile (Edit1. Text);
Image2. Picture. Bitmap. Assign (Image1. Picture. Graphic);
Image3. Picture. Bitmap. Width:=332;
Image3. Picture. Bitmap. Height:=212;
Image3. Picture. Bitmap. Canvas. CopyRect (the Rect (0,0,332,212), Image2. The Picture. The Bitmap. The Canvas, the Rect (0,0,332,212));
end;

Thank you very much, is a tbitmap sbeijing, not timage, I forgot to say the copy function is operated in another thread, just find a similar CSDN from http://bbs.csdn.net/topics/90123608, says it is because of the reason of multithreading, Delphi is used in the Synchronize complains, so I use a semaphore locking, mutual exclusion of critical section, but still there will be a white picture,,,

CodePudding user response:

You'd better specified when creating the Bitmap PixelFormat,

CodePudding user response:

Try to switch to HBITMP also not wrong

CodePudding user response:

I also encountered similar problems, I do is a remote screen, the service side of screen generated bitmap, then compressed into a jpeg and send to the client, the test is found in the screen sometimes generated bitmap cannot use SaveToFile method, prompt resources beyond, but the bitmap after converted to jpeg, jpeg can be saved, and the image is correct, then try to use BitBlt'll copy the bitmap to a bitmap, the other a bitmap image can be saved, but the content is empty, the last don't BitBlt, switch to ScanLline method the bitmap kao to another a bitmap, the other a bitmap image can be saved, the image is also correct, really very strange,
Gave me the feeling of two, a like a screen shot (with BitBlt) is a question of timing, may in some cases can't screen (when I test fast moving window, can appear the above question), but so far no argument, another feeling BitBlt copies image is indeed very fast, but if you don't succeed sometimes (of course there could be a TBitmap Canvas itself problem or Canvas is a problem with the Handle itself), but also no test out,
  • Related