博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Demo: 模仿画图程序的选块另存为功能
阅读量:5451 次
发布时间:2019-06-15

本文共 7020 字,大约阅读时间需要 23 分钟。

Demo: 模仿画图程序的选块另存为功能

分类: 2007-06-16 10:31

 转自:

(****************************************

  单元名称: 模仿画图程序的选块另存为功能
 
    : LihuaSoft
 
编码日期: 2007616
 
    : 因此程序是演示程序, 只允许使用
            BMP
格式图片
*****************************************)

unit Unit1;

interface

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, jpeg, Menus;

type

  TForm1 = class(TForm)
    Button1: TButton;
    Image1: TImage;
    PopupMenu1: TPopupMenu;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
    Bmp : TBitmap;
    rFrom, rTo : TRect;
    CanSelect : boolean;
    MenuItemSaveAs : TMenuItem;
    procedure SelectedSaveAs(Sender: TObject);
  public
    { Public declarations }
  end;

var

  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);

begin
  Caption := '演示:导入一幅BMP后,拖动鼠标左键选择一块区域,右键另存为';
  Button1.Caption := '
导入BMP';
  Image1.Left := 0;
  Image1.Top := 0;
  Image1.AutoSize := True;
  OpenDialog1.FileName := '*.BMP';
  OpenDialog1.Filter := 'Windows
位图文件';
  SaveDialog1.FileName := '*.BMP';
  SaveDialog1.Filter := '
请输入欲存为的BMP文件名';
  MenuItemSaveAs := TMenuItem.Create(PopupMenu1);
  MenuItemSaveAs.Caption := '
选区另存为BMP文件';
  MenuItemSaveAs.OnClick := SelectedSaveAs;
  PopupMenu1.Items.Add(MenuItemSaveAs);
end;

procedure TForm1.Button1Click(Sender: TObject);

begin
  if OpenDialog1.Execute then begin
    if Bmp<>nil then FreeAndNil(Bmp);
    Bmp := TBitmap.Create;
    Bmp.LoadFromFile(OpenDialog1.FileName);
    with Image1 do begin
      Picture.Bitmap := Bmp;
      Canvas.Pen.Mode := pmNot;
      Canvas.Brush.Style := bsClear;
      OnMouseDown := Image1MouseDown;
      OnMouseMove := Image1MouseMove;
      OnMouseUp := Image1MouseUp;
    end;
    rFrom := Rect(0, 0, 0, 0);
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);

begin
  if Bmp<>nil then FreeAndNil(Bmp);
  if MenuItemSaveAs<>nil then FreeAndNil(MenuItemSaveAs);
end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;

  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then begin
    CanSelect := True;
    if (rFrom.Left<>rFrom.Right) and (rFrom.Top<>rFrom.Bottom) then
      Image1.Canvas.Rectangle(rFrom);
    rFrom.Left := X;
    rFrom.Top := Y;
    rFrom.Right := X;
    rFrom.Bottom := Y;
  end;
  if Button = mbRight then
    if (rFrom.Left<>rFrom.Right) and (rFrom.Top<>rFrom.Bottom) then
      PopupMenu1.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y);
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,

  Y: Integer);
begin
  if CanSelect then begin
    Image1.Canvas.Rectangle(rFrom);
    rFrom.Right := X;
    rFrom.Bottom := Y;
    Image1.Canvas.Rectangle(rFrom);
  end;
end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;

  Shift: TShiftState; X, Y: Integer);
begin
  CanSelect := False;
end;

procedure TForm1.SelectedSaveAs(Sender: TObject);

var
  TempBmp : TBitmap;
begin
  if SaveDialog1.Execute then begin
    TempBmp := TBitmap.Create;
    try
      TempBmp.Width := rFrom.Right-rFrom.Left;
      TempBmp.Height := rFrom.Bottom-rFrom.Top;
      rTo := Rect(0, 0, TempBmp.Width, TempBmp.Height);
      TempBmp.Canvas.CopyRect(rTo, Bmp.Canvas, rFrom);
      TempBmp.SaveToFile(SaveDialog1.FileName);
    finally
      TempBmp.Free;
    end;
  end;
end;

end.

 

======================

 

200892新增适用JPG格式代码:

unit Unit1;

interface

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, jpeg, Menus;

type

  TForm1 = class(TForm)
    Button1: TButton;
    Image1: TImage;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    PopupMenu1: TPopupMenu;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
    Bmp : TBitmap;
    Jpg : TJpegImage;
    rFrom, rTo : TRect;
    CanSelect : boolean;
    MenuItemSaveAs : TMenuItem;
    procedure SelectedSaveAs(Sender: TObject);
  public
    { Public declarations }
  end;

var

  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);

begin
  Caption := '演示:导入一幅JPG后,拖动鼠标左键选择一块区域,右键另存为';
  Button1.Caption := '
导入JPG';
  Image1.Left := 0;
  Image1.Top := 0;
  Image1.AutoSize := True;
  OpenDialog1.FileName := '*.JPG';
  OpenDialog1.Filter := 'Jpeg
格式文件';
  SaveDialog1.FileName := '*.JPG';
  SaveDialog1.Filter := '
请输入欲存为的JPG文件名';
  MenuItemSaveAs := TMenuItem.Create(PopupMenu1);
  MenuItemSaveAs.Caption := '
选区另存为JPG文件';
  MenuItemSaveAs.OnClick := SelectedSaveAs;
  PopupMenu1.Items.Add(MenuItemSaveAs);
end;

procedure TForm1.Button1Click(Sender: TObject);

begin
  if OpenDialog1.Execute then begin
    if not Assigned(Bmp) then Bmp := TBitmap.Create;
    if not Assigned(Jpg) then Jpg := TJpegImage.Create;
    Jpg.LoadFromFile(OpenDialog1.FileName);
    Bmp.Assign(Jpg);
    with Image1 do begin
      Picture.Bitmap := Bmp;
      Canvas.Pen.Mode := pmNot;
      Canvas.Brush.Style := bsClear;
      OnMouseDown := Image1MouseDown;
      OnMouseMove := Image1MouseMove;
      OnMouseUp := Image1MouseUp;
    end;
    rFrom := Rect(0, 0, 0, 0);
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);

begin
  if Bmp<>nil then FreeAndNil(Bmp);
  if Jpg<>nil then FreeAndNil(Jpg);
  if MenuItemSaveAs<>nil then FreeAndNil(MenuItemSaveAs);
end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;

  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then begin
    CanSelect := True;
    if (rFrom.Left<>rFrom.Right) and (rFrom.Top<>rFrom.Bottom) then
      Image1.Canvas.Rectangle(rFrom);
    rFrom.Left := X;
    rFrom.Top := Y;
    rFrom.Right := X;
    rFrom.Bottom := Y;
  end;
  if Button = mbRight then
    if (rFrom.Left<>rFrom.Right) and (rFrom.Top<>rFrom.Bottom) then
      PopupMenu1.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y);
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,

  Y: Integer);
begin
  if CanSelect then begin
    Image1.Canvas.Rectangle(rFrom);
    rFrom.Right := X;
    rFrom.Bottom := Y;
    Image1.Canvas.Rectangle(rFrom);
  end;
end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;

  Shift: TShiftState; X, Y: Integer);
begin
  CanSelect := False;
end;

procedure TForm1.SelectedSaveAs(Sender: TObject);

var
  TempBmp : TBitmap;
  TempJpg : TJpegImage;
begin
  if SaveDialog1.Execute then begin
    TempBmp := TBitmap.Create;
    TempJpg := TJpegImage.Create;
    try
      TempBmp.Width := rFrom.Right-rFrom.Left;
      TempBmp.Height := rFrom.Bottom-rFrom.Top;
      rTo := Rect(0, 0, TempBmp.Width, TempBmp.Height);
      TempBmp.Canvas.CopyRect(rTo, Bmp.Canvas, rFrom);
      TempJpg.Assign(TempBmp);
      TempJpg.SaveToFile(SaveDialog1.FileName);
    finally
      TempJpg.Free;
      TempBmp.Free;
    end;
  end;
end;

end.

 

 

转载于:https://www.cnblogs.com/hssbsw/archive/2012/06/15/2551441.html

你可能感兴趣的文章
HashMap实现原理分析
查看>>
Symantec AntiVirus企业版联机客户机端卸载密码(转)
查看>>
jQuery中的ajax
查看>>
BPM实例分享:H3如何调试V3
查看>>
程序员讨论《黑客帝国》(一)真实与虚拟
查看>>
flex布局
查看>>
【C++ 拾遗】C++'s most vexing parse
查看>>
Codeforces 1C Ancient Berland Circus
查看>>
SGU 275 To xor or not to xor
查看>>
[学习笔记]设计模式之Factory Method
查看>>
删除过期备份报错RMAN-06207 RMAN-06208解决方案
查看>>
创意无限!一组网页边栏过渡动画【附源码下载】
查看>>
Java_Hello World
查看>>
建造者模式
查看>>
Python:tesserocr 在 windows 下的安装及简单使用
查看>>
周周总结——时时更新(第4学期,第4周)
查看>>
在ubuntu12.04,64位中安装lnmp一键包mysql的问题
查看>>
一级关联数组转化成多层子级数组
查看>>
百度Ueditor编辑器的Html模式自动替换样式的解决方法
查看>>
八:Razor(MVC框架视图引擎)
查看>>