You are on page 1of 19

1.

Membaca file berekstensi JPG

KODING

unit UnitUtama;

interface

uses

Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,


Vcl.Graphics,

Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtDlgs, Vcl.ExtCtrls, Vcl.Buttons,

Vcl.Grids, Vcl.DBCtrls, Vcl.Mask, jpeg, axCtrls,

Data.Win.ADODB, Vcl.ComCtrls;

type

TFormUtama = class(TForm)

OpenPictureDialog: TOpenPictureDialog;

PanelAtas: TPanel;

ButtonAmbilCitra: TButton;

StatusBar: TStatusBar;

ButtonAmbilCitra2: TButton;

OpenPictureDialog2: TOpenPictureDialog;

StatusBar2: TStatusBar;

procedure ButtonAmbilCitraClick(Sender: TObject);

procedure ButtonAmbilCitra2Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }
procedure convertobmp(filename:TFileName);

end;

var

FormUtama: TFormUtama;

implementation

uses UnitCitra;

{$R *.dfm}

procedure TFormUtama.ButtonAmbilCitra2Click(Sender: TObject);

var

fc : string;

b1 : TBitmap;

p1 : TPicture;

// a : TStream;

begin

if (OpenPictureDialog2.Execute()) then

begin

if (FormCitra = nil) then

Application.CreateForm(TFormCitra, FormCitra);

if (LowerCase(ExtractFileExt(OpenPictureDialog2.FileName)) = '.bmp') then

FormCitra.Image2.Picture.LoadFromFile(OpenPictureDialog2.FileName)

else if (LowerCase(ExtractFileExt(OpenPictureDialog2.FileName)) = '.jpg') then

begin
convertobmp(OpenPictureDialog2.FileName);

end

else

begin

p1 := TPicture.Create;

p1.LoadFromFile(OpenPictureDialog2.FileName);

b1 := TBitmap.Create;

b1.Height := p1.Graphic.Height;

b1.Width := p1.Graphic.Width;

b1.Canvas.Draw(0, 0, p1.Graphic);

b1.PixelFormat := pf24bit;

FormCitra.Image2.Picture.Bitmap := b1;

p1.Free;

end;

FormCitra.ClientHeight := FormCitra.Image2.Picture.Height;

FormCitra.ClientWidth := FormCitra.Image2.Picture.Width;

FormCitra.ClientHeight := FormCitra.Image2.Picture.Height;

case (FormCitra.Image2.Picture.Bitmap.PixelFormat) of

pf1bit : fc := ', biner';

pf8bit : fc := ', keabuan';

pf24bit : fc := ', true color';

end;

StatusBar2.SimpleText := OpenPictureDialog2.FileName+' ('+

IntToStr((FormCitra.Image2.Picture.Height))+'x'+

IntToStr((FormCitra.Image2.Picture.Width))+

fc+')';

end;

end;
procedure TFormUtama.ButtonAmbilCitraClick(Sender: TObject);

var

fc : string;

b1 : TBitmap;

p1 : TPicture;

a : TStream;

jpg: TJpegImage;

begin

if (OpenPictureDialog.Execute()) then

begin

if (FormCitra = nil) then

Application.CreateForm(TFormCitra, FormCitra);

if (LowerCase(ExtractFileExt(OpenPictureDialog.FileName)) = '.bmp') then

FormCitra.Image.Picture.LoadFromFile(OpenPictureDialog.FileName)

else if (LowerCase(ExtractFileExt(OpenPictureDialog.FileName)) = '.jpg') then

begin

convertobmp(OpenPictureDialog.FileName);

end

else

begin

p1 := TPicture.Create;

p1.LoadFromFile(OpenPictureDialog.FileName);

b1 := TBitmap.Create;

b1.Height := p1.Graphic.Height;

b1.Width := p1.Graphic.Width;

b1.Canvas.Draw(0, 0, p1.Graphic);

b1.PixelFormat := pf24bit;
FormCitra.Image.Picture.Bitmap := b1;

p1.Free;

end;

FormCitra.ClientHeight := FormCitra.Image.Picture.Height;

FormCitra.ClientWidth := FormCitra.Image.Picture.Width;

FormCitra.ClientHeight := FormCitra.Image.Picture.Height;

case (FormCitra.Image.Picture.Bitmap.PixelFormat) of

pf1bit : fc := ', biner';

pf8bit : fc := ', keabuan';

pf24bit : fc := ', true color';

end;

StatusBar.SimpleText := OpenPictureDialog.FileName+' ('+

IntToStr((FormCitra.Image.Picture.Height))+'x'+

IntToStr((FormCitra.Image.Picture.Width))+

fc+')';

end;

end;

procedure TFormUtama.convertobmp(filename: TFileName);

var OleGraphic : TOleGraphic;

fs : TFileStream;

Source : TImage;

BMP : TBitmap;

Begin

Try

OleGraphic := TOleGraphic.Create; //The magic class!

fs := TFileStream.Create(filename, fmOpenRead Or fmSharedenyNone);


OleGraphic.LoadFromStream(fs);

Source := Timage.Create(Nil);

Source.Picture.Assign(OleGraphic);

BMP := TBitmap.Create; //Converting to Bitmap

bmp.Width := Source.Picture.Width;

bmp.Height := source.Picture.Height;

bmp.Canvas.Draw(0, 0, source.Picture.Graphic);

FormCitra.Image.Picture.Bitmap := bmp; //Show the bitmap on form

FormCitra.Image.Refresh;

fs.Free;

OleGraphic.Free;

Source.Free;

bmp.Free;

Finally

End;

end;

end.
2.Hanya bias menyimpan file menjadi gambar berekstensi BMP

unit UnitUtama;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, ComCtrls, ExtDlgs, StdCtrls, ExtCtrls, jpeg, axCtrls;

type

Tformutama = class(TForm)

PanelAtas: TPanel;

ButtonAmbilCitra: TButton;

OpenPictureDialog: TOpenPictureDialog;

StatusBar: TStatusBar;

Button1: TButton;

OpenPictureDialog1: TOpenPictureDialog;

StatusBar1: TStatusBar;

Button2: TButton;

Button3: TButton;

Image1: TImage;

SavePictureDialog1: TSavePictureDialog;

procedure ButtonAmbilCitraClick(Sender: TObject);

procedure Button1Click(Sender: TObject);

procedure convertobmp(filename:TFileName);

procedure Button2Click(Sender: TObject);

procedure Button3Click(Sender: TObject);


private

{ Private declarations }

public

{ Public declarations }

// procedure convertobmp(filename:TFileName);

end;

var

formutama: Tformutama;

fc : string;

b1,b2 : TBitmap;

p1,p2 : TPicture;

a : TStream;

jpg: TJpegImage;

implementation

uses UnitCitra;

{$R *.dfm}

procedure Tformutama.Button1Click(Sender: TObject);

begin

if (OpenPictureDialog1.Execute) then
begin

p2:= TPicture.Create;

p2.LoadFromFile(OpenPictureDialog1.FileName);

b2:= TBitmap.Create;

b2.Height := p2.Graphic.Height;

b2.Width := p2.Graphic.Width;

b2.Canvas.Draw(0,0,p2.Graphic);

b2.PixelFormat := pf24bit;

FormCitra.Gambar.Picture.Bitmap := b2;

p2.Free;

case(FormCitra.gambar.Picture.Bitmap.PixelFormat) of

pf1bit : fc := ',biner';

pf8bit : fc := ',keabuan';

pf24bit : fc := ',true color';

end;

StatusBar1.SimpleText := OpenPictureDialog1.FileName+' ('+

IntToStr((FormCitra.Gambar.Picture.Height))+'x' +

IntToStr((FormCitra.Gambar.Picture.Width))+', ' + fc +')';

end;

end;

procedure Tformutama.Button2Click(Sender: TObject);

var foto : TMemorystream;

begin

foto := TMemorystream.Create;

begin

if SavePictureDialog1.Execute then;

formcitra.image.Picture.SaveToFile(savepicturedialog1.FileName+'.bmp');
end;

end;

procedure Tformutama.Button3Click(Sender: TObject);

begin

application.Terminate;

end;

procedure Tformutama.ButtonAmbilCitraClick(Sender: TObject);

begin

if (OpenPictureDialog.Execute) then

begin

if (FormCitra = nil) then

Application.CreateForm(TFormCitra, FormCitra);

if (LowerCase(ExtractFileExt(OpenPictureDialog.FileName)) = '.bmp') then

FormCitra.Image.Picture.LoadFromFile(OpenPictureDialog.FileName)

else if (LowerCase(ExtractFileExt(OpenPictureDialog.FileName)) = '.jpg') then

begin

convertobmp(OpenPictureDialog.FileName);

end

else

begin

p1:= TPicture.Create;

p1.LoadFromFile(OpenPictureDialog.FileName);

b1:= TBitmap.Create;

b1.Height := p1.Graphic.Height;

b1.Width := p1.Graphic.Width;

b1.Canvas.Draw(0,0,p1.Graphic);

b1.PixelFormat := pf24bit;

FormCitra.Image.Picture.Bitmap := b1;
p1.Free;

end;

FormCitra.ClientHeight := 500;

FormCitra.ClientWidth := 500;

case(FormCitra.Image.Picture.Bitmap.PixelFormat) of

pf1bit : fc := 'biner';

pf8bit : fc := 'keabuan';

pf24bit : fc := 'true color';

end;

StatusBar.SimpleText := OpenPictureDialog.FileName+' ('+

IntToStr((FormCitra.Image.Picture.Height))+'x' +

IntToStr((FormCitra.Image.Picture.Width))+', ' + fc +')';

end;

end;

procedure TFormUtama.convertobmp(filename: TFileName);

var OleGraphic : TOleGraphic;

fs : TFileStream;

Source : TImage;

BMP : TBitmap;

Begin

Try

OleGraphic := TOleGraphic.Create; //The magic class!

fs := TFileStream.Create(filename, fmOpenRead Or fmSharedenyNone);

OleGraphic.LoadFromStream(fs);

Source := Timage.Create(Nil);

Source.Picture.Assign(OleGraphic);

BMP := TBitmap.Create; //Converting to Bitmap


bmp.Width := Source.Picture.Width;

bmp.Height := source.Picture.Height;

bmp.Canvas.Draw(0, 0, source.Picture.Graphic);

FormCitra.Image.Picture.Bitmap := bmp; //Show the bitmap on form

FormCitra.Image.Refresh;

fs.Free;

OleGraphic.Free;

Source.Free;

bmp.Free;

Finally

End;

end;

end.
3. Dapat menampilkan 2 gambar

unit UnitUtama;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, ComCtrls, ExtDlgs, StdCtrls, ExtCtrls, jpeg, axCtrls;

type

Tformutama = class(TForm)

PanelAtas: TPanel;

ButtonAmbilCitra: TButton;

OpenPictureDialog: TOpenPictureDialog;

StatusBar: TStatusBar;

Button1: TButton;

OpenPictureDialog1: TOpenPictureDialog;

StatusBar1: TStatusBar;

Button3: TButton;

Image1: TImage;

SavePictureDialog1: TSavePictureDialog;

procedure ButtonAmbilCitraClick(Sender: TObject);

procedure Button1Click(Sender: TObject);

procedure convertobmp(filename:TFileName);

procedure Button3Click(Sender: TObject);


private

{ Private declarations }

public

{ Public declarations }

// procedure convertobmp(filename:TFileName);

end;

var

formutama: Tformutama;

fc : string;

b1,b2 : TBitmap;

p1,p2 : TPicture;

a : TStream;

jpg: TJpegImage;

implementation

uses UnitCitra;

{$R *.dfm}

procedure Tformutama.Button1Click(Sender: TObject);

begin

if (OpenPictureDialog1.Execute) then

begin
p2:= TPicture.Create;

p2.LoadFromFile(OpenPictureDialog1.FileName);

b2:= TBitmap.Create;

b2.Height := p2.Graphic.Height;

b2.Width := p2.Graphic.Width;

b2.Canvas.Draw(0,0,p2.Graphic);

b2.PixelFormat := pf24bit;

FormCitra.Gambar.Picture.Bitmap := b2;

p2.Free;

case(FormCitra.gambar.Picture.Bitmap.PixelFormat) of

pf1bit : fc := ',biner';

pf8bit : fc := ',keabuan';

pf24bit : fc := ',true color';

end;

StatusBar1.SimpleText := OpenPictureDialog1.FileName+' ('+

IntToStr((FormCitra.Gambar.Picture.Height))+'x' +

IntToStr((FormCitra.Gambar.Picture.Width))+', ' + fc +')';

end;

end;

procedure Tformutama.Button3Click(Sender: TObject);

begin

application.Terminate;
end;

procedure Tformutama.ButtonAmbilCitraClick(Sender: TObject);

begin

if (OpenPictureDialog.Execute) then

begin

if (FormCitra = nil) then

Application.CreateForm(TFormCitra, FormCitra);

if (LowerCase(ExtractFileExt(OpenPictureDialog.FileName)) = '.bmp') then

FormCitra.Image.Picture.LoadFromFile(OpenPictureDialog.FileName)

else if (LowerCase(ExtractFileExt(OpenPictureDialog.FileName)) = '.jpg') then

begin

convertobmp(OpenPictureDialog.FileName);

end

else

begin

p1:= TPicture.Create;

p1.LoadFromFile(OpenPictureDialog.FileName);

b1:= TBitmap.Create;

b1.Height := p1.Graphic.Height;

b1.Width := p1.Graphic.Width;

b1.Canvas.Draw(0,0,p1.Graphic);

b1.PixelFormat := pf24bit;

FormCitra.Image.Picture.Bitmap := b1;

p1.Free;

end;

FormCitra.ClientHeight := 500;

FormCitra.ClientWidth := 500;
case(FormCitra.Image.Picture.Bitmap.PixelFormat) of

pf1bit : fc := 'biner';

pf8bit : fc := 'keabuan';

pf24bit : fc := 'true color';

end;

StatusBar.SimpleText := OpenPictureDialog.FileName+' ('+

IntToStr((FormCitra.Image.Picture.Height))+'x' +

IntToStr((FormCitra.Image.Picture.Width))+', ' + fc +')';

end;

end;

procedure TFormUtama.convertobmp(filename: TFileName);

var OleGraphic : TOleGraphic;

fs : TFileStream;

Source : TImage;

BMP : TBitmap;

Begin

Try

OleGraphic := TOleGraphic.Create; //The magic class!

fs := TFileStream.Create(filename, fmOpenRead Or fmSharedenyNone);

OleGraphic.LoadFromStream(fs);

Source := Timage.Create(Nil);

Source.Picture.Assign(OleGraphic);

BMP := TBitmap.Create; //Converting to Bitmap

bmp.Width := Source.Picture.Width;

bmp.Height := source.Picture.Height;
bmp.Canvas.Draw(0, 0, source.Picture.Graphic);

FormCitra.Image.Picture.Bitmap := bmp; //Show the bitmap on form

FormCitra.Image.Refresh;

fs.Free;

OleGraphic.Free;

Source.Free;

bmp.Free;

Finally

End;

end;

end.

You might also like