I haven’t written anything for a long time, not because I suffered a lot of tomatoes and eggs in my previous posts, but because I really don’t have anything real to write down for everyone’s reference. But recently when I was helping a friend with a project, I came across a simple process of image processing in Delphi, so I kept looking for information on the Internet and looking through previous csdn posts. From my search results, I found that many people would I have encountered similar image processing problems in various projects, but most of the answers seem to be incomplete (because many of them have not been posted, or it may be a matter of personal habits. Haha, I hope that after reading this article of mine, everyone can start working on it at home. Post closing speed). So I wrote the problems and solutions I encountered below for your reference. At the same time, you are welcome to continue to make bricks.
I need to write down the problem I want to solve before the formal content, and also give readers a good position so that they will not be annoyed by my poor handling of the problem. The main problem to be solved here is to rotate the image scanned with the scanner 90 degrees in Delphi to obtain a suitable image for use, and because the size of the image cannot be correctly grasped, the image needs to be simply scaled. And my solution is completely based on this requirement, because I don't think much about efficiency and clarity after image processing, and the benefit is that the program looks very simple.
First of all, I don't have a scanner, so I don't know what format the picture obtained by using the scanner (already provided) is, and in Delphi's help it says "A bitmap is a powerful graphics object used to create, manipulate (scale, scroll, rotate, and paint), and store images in memory and as files on a disk", so my first step is to convert my slices into TBitmap for processing. While converting the format, I changed the actual size of the image to suit the needs of later selecting part of the image. The function is as follows:
PRocedure TMainForm.ChangeImageFormate;
var Bitmap : TBitmap;
Zoom: Integer;
begin
Bitmap := TBitmap.Create;//1
try
with ImageCert do begin
Bitmap.Assign(Picture.Graphic);//2
Picture := nil;//3
Zoom := Max(Bitmap.Width div Width,Bitmap.Height div Height)+1;//4
Width := Bitmap.Width div Zoom;//5
Height := Bitmap.Height div Zoom;//6
Canvas.StretchDraw(Rect(0,0,Width,Height),Bitmap);//7
end;
finally
Bitmap.Free;
end;
end;
I think this code is not very complicated. ImageCert is a TImage control placed on the Form. The only thing that may be unfamiliar is the eighth sentence, but you can get a lot of explanations from Delphi's help. I won't translate it here. (My advice is to read more help). At the same time, it should be pointed out that the third sentence is very critical. If you remove this sentence and your image format is not bmp, the error "Only bitmap can be modified" will appear.
The second step is to realize the rotation of the image, because the program requires only a 90-degree rotation each time, so it is easier to handle here. The method of processing is to replace according to pixels. The implementation process is as follows:
procedure TMainForm.RotateImage;
var x,y : Integer;
TmpBitMap : TBitmap;
begin
TmpBitMap := TBitmap.Create;
try
TmpBitMap.Assign(ImageCert.Picture.Graphic);
with ImageCert do begin
Picture.Bitmap.Height := TmpBitMap.Width;
Picture.Bitmap.Width := TmpBitMap.Height;
for x:=0 to Height do
for y:=0 to Width do
Canvas.Pixels[TmpBitMap.Height-x,y] := TmpBitMap.Canvas.Pixels[y,x];
Height := TmpBitMap.Width;
Width := TmpBitMap.Height;
end;
finally
TmpBitMap.Free;
end;
end;
There is nothing much to talk about in this program, but the several height and width settings make me very annoyed. I always feel that I need to do something more, but I can't remove any rows. If anyone is interested, you can try it. It would be best if you tell me the results. And in this case, since TImage and TBitmap have their own canvas, which one to use may be worth studying, but due to limited time, I have not tried it.
As for part of the image taking process, I used a TShape, then set the brush style to bsclear, and arranged the position of the TShape in onMouseMove of ImageCert. This is not very sensitive because when the mouse is on the TShape It will not have any effect, but fortunately the impact will be small. At the same time, I took out some pictures under TShape in the OnMouseDown event of TShape. At this time, if your image has not been scaled, you will find that the removed image does not match the position we see (of course, when TImage does not display the image size correctly).