Today, Delphi is one of the popular desktop programming tool. There are thousand even million programmer in this world using delphi as their favorit tool. This site try to collect the examples, tips and tricks of Delphi programming. We collect, test them and redistribute the collection of delphi programming for you.

Bitmap Operation for JPEG or JPG Image

Author: admin | Category: Files, Graphic, Tips and Tricks

//This is how we can operate any Bitmap Operation to our JPEG image
//To make a bitmap operation in JPEG Image, first we need to convert JPEG Image to bitmap
//Then run any bitmap operation on convert result bitmap
//Then assign JPEG result from operated bitmap
//For this example we use convert to grayscale bitmap operation
//Use Button1(TButton) and OpenDialog1(TOpenDialog) for this example
 
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
uses
  jpeg;
 
{$R *.dfm}
 
function ConvertBitmapToGrayscale(const Bitmap: TBitmap): TBitmap;
var
  i, j: Integer;
  Grayshade, Red, Green, Blue: Byte;
  PixelColor: Longint;
begin
  with Bitmap do
    for i := 0 to Width - 1 do
      for j := 0 to Height - 1 do
      begin
        PixelColor := ColorToRGB(Canvas.Pixels[i, j]);
        Red        := PixelColor;
        Green      := PixelColor shr 8;
        Blue       := PixelColor shr 16;
        Grayshade  := Round(0.3 * Red + 0.6 * Green + 0.1 * Blue);
        Canvas.Pixels[i, j] := RGB(Grayshade, Grayshade, Grayshade);
      end;
  Result := Bitmap;
end;
 
procedure ConvertToBitmap(Source : TGraphic; Bitmap : TBitmap);
begin
 try
  if Bitmap = nil then
    Bitmap := TBitmap.Create
  else Bitmap.FreeImage;
  Bitmap.Width := Source.Width;
  Bitmap.Height := Source.Height;
  Bitmap.Canvas.Draw(0,0,Source);
 except
   showmessage('No Loaded File');
 end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  MyJpeg,OperatedJpeg:TJPEGImage;
  MyBitmap,OperatedBitmap:TBitmap;
begin
  if OpenDialog1.Execute then
  begin
    MyJpeg        := TJPEGImage.Create;
    MyBitmap      := TBitmap.Create;
    OperatedBitmap := TBitmap.Create;
    OperatedJpeg   :=  TJPEGImage.Create;
    try
      MyJpeg.LoadFromFile(OpenDialog1.FileName);
      ConvertToBitmap(MyJpeg,MyBitmap);
      // Do any Bitmap Operation Here
      //For Example i Convert Bitmap To GrayScale and perform result into
      //operateBitmap
      OperatedBitmap := ConvertBitmapToGrayscale(MyBitmap);
      //Perform operated bitmap into jpeg
      OperatedJpeg.Assign(OperatedBitmap);
      OperatedJpeg.SaveToFile(OpenDialog1.FileName+'-Gray.jpeg');
      Showmessage('Operation Completed')
    finally
      MyJpeg.Free;
      OperatedJpeg.Free;
    end;
  end;
end;
 
end.

Tags: , , , ,