|
Convert JPEG to BMP and vice versa - there is nothing essentially a complex. Jpeg is used to compress the image, but here there is the disadvantage that if you use very high compression ratio for the jpeg-files, the image is just simply could break (begin to fall) into blocks of pixels. In bmp-images for display provides a color assigned from 1 to 48 bits. Probably the most commonly used - is 24 bits at 48 bits - This format is rarely used with high color accuracy and, accordingly, it will be much larger in size than the same with 24 bits of color, and so the data transfer (conversion) we will reduce the image size and different. Let us then install all the necessary components: * TGroupBox - 2 pcs * TEdit - 2 pcs * TLabel - 2 pcs * TButton - 6 pieces * TOpendialog * TSaveDialog I n one block, we will convert jpeg to bmp, and in another unit vice-versa. In addition, we need to connect the module uses jpeg, so we can work with jpeg-images in Delphi. After that, we will understand how to convert BMP to JPEG. Now write the code to open a bmp-file. Source code procedure TForm1.Button1Click (Sender: TObject); begin if OpenDialog1.Execute then Edit1.Text: = OpenDialog1.FileName else exit; end; And now the code that will specify the name, is retained (output) jpeg-file. Source code procedure TForm1.Button2Click (Sender: TObject); begin if SaveDialog1.Execute then begin Edit2.Text: = SaveDialog1.FileName + '. Jpg'; end else exit; end; Well, and now has begun to convert most of our images on the buttons OnClick write the code Source code procedure TForm1.Button3Click (Sender: TObject); var jpg: TJPEGImage; bmp: TBitmap; begin try jpg: = TJPEGImage.Create; bmp: = TBitmap.Create; bmp.LoadFromFile (Edit1.Text); jpg.Assign (bmp); jpg.SaveToFile (Edit2.Text); jpg.Free; FreeAndNil (bmp); except on e: Exception do begin jpg.Free; bmp.Free; end; end; end; Do not forget to plug in uses jpeg. We create an instance of TGraphic - jpeg and bmp. Then we load a bmp image that we need to convert, and then use the Assign save it in jpeg, after which the previously created the file, save our jpeg-file on disk. Now you can test very easily, if no bmp-images, then it is very easy to create - open mspaint and draw a couple lines and save as bmp-image, then open it and click on the button "Convert", compare their size - differ greatly. We now proceed to the opposite effect - Convert JPEG to BMP. Code to open the jpeg-file the same Source code procedure TForm1..Button4Click (Sender: TObject); begin, if OpenDialog1.Execute then Edit3.Text: = OpenDialog1.FileName else exit; end; To preserve nearly the same Source code procedure TForm1.Button5Click (Sender: TObject); begin if SaveDialog1.Execute then Edit4.Text: = SaveDialog1.FileName else exit; end; And now the code that converts our image Source code procedure TForm1.Button6Click (Sender: TObject); var jpg: TJPEGImage; bmp: TBitmap;begin; try jpg: = TJPEGImage.Create; bmp: = TBitmap.Create; jpg.CompressionQuality: = 100; jpg.Compress; jpg.LoadFromFile (Edit3.Text); bmp.Assign (jpg); bmp.SaveToFile (ChangeFileExt (Edit4.Text, '. bmp')); FreeAndNil (jpg); FreeAndNil (bmp); except on e: Exception do; begin; FreeAndNil (jpg); FreeAndNil (bmp); end; end;end;
|
No Responses to Convert Jpeg To Bmp