delphi fmx jpg 保存数据库 从数据库读取  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi fmx jpg 保存数据库 从数据库读取


unit uJpgSaveToData;

interface

uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
    Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtDlgs, Vcl.StdCtrls,
    Vcl.Mask, scGPExtControls, Vcl.ExtCtrls, FireDAC.Stan.Intf, FireDAC.Stan.Option,
    FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
    FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.MSSQL,
    FireDAC.Phys.MSSQLDef, FireDAC.VCLUI.Wait, FireDAC.Stan.Param, FireDAC.DatS,
    FireDAC.DApt.Intf, FireDAC.DApt, cxGraphics, cxControls, cxLookAndFeels,
    cxLookAndFeelPainters, cxStyles, cxCustomData, cxFilter, cxData,
    cxDataStorage, cxEdit, cxNavigator,
    cxDataControllerConditionalFormattingRulesManagerDialog, Data.DB, cxDBData,
    cxGridLevel, cxClasses, cxGridCustomView, cxGridCustomTableView,
    cxGridTableView, cxGridDBTableView, cxGrid, FireDAC.Comp.DataSet, FireDAC.Comp.Client,
    scControls, scGPImages, cxContainer, cxImage, Data.Win.ADODB, cxBlobEdit;

type
    TForm1 = class(TForm)
        edt_JPG_Add: TscGPEdit;
        OpenPictureDialog1: TOpenPictureDialog;
        SaveToData: TscButton;
        ReadImage: TscButton;
        FDConnection1: TFDConnection;
        FDQuery1: TFDQuery;
        DataSource1: TDataSource;
        FDQuery2: TFDQuery;
        cxGrid1DBTableView1: TcxGridDBTableView;
        cxGrid1Level1: TcxGridLevel;
        cxGrid1: TcxGrid;
        editId: TscGPEdit;
        cxGrid1DBTableView1ID: TcxGridDBColumn;
        cxGrid1DBTableView1Photo: TcxGridDBColumn;
        cxGrid1DBTableView1NameBM: TcxGridDBColumn;
        scButton1: TscButton;
        Image2: TImage;
        Image1: TImage;
        scMemo1: TscMemo;
        procedure edt_JPG_AddRightButtonClick(Sender: TObject);
        procedure SaveToDataClick(Sender: TObject);
        procedure ReadImageClick(Sender: TObject);
        procedure scButton1Click(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;

var
    Form1: TForm1;

implementation

{$R *.dfm}
uses
    jpeg;

procedure TForm1.ReadImageClick(Sender: TObject);
// 读取图片
var
    MemoStream: TMemoryStream;
    Jpg: TJPEGImage;
begin
    FDQuery1.Close;
    FDQuery1.SQL.Text := 'SELECT * FROM bTest where NameBM =' + editId.Text;
   // 查询图片
    FDQuery1.Open;
    if not FDQuery1.FieldByName('photo').IsNull then
    begin
        MemoStream := TMemoryStream.Create;
        Jpg := TJPEGImage.Create;
        TBlobField(FDQuery1.FieldByName('Photo')).SaveToStream(MemoStream);    // 显示的转换为BlobField并保存到内存流
        MemoStream.Position := 0;
        Jpg.LoadFromStream(MemoStream);    // 加载图片
        image2.Picture.Assign(Jpg);
    end
    else
    begin
        image2.Picture := nil;
    end;
    MemoStream.Free;
    Jpg.Free;

end;

procedure TForm1.SaveToDataClick(Sender: TObject);
var
    MemoStream: TMemoryStream;
    Jpg: TJPEGImage;
begin
    try
        MemoStream := TMemoryStream.Create; //  创建内存流
        Jpg := TJPEGImage.Create;
       // Image1.Picture.Graphic.SaveToStream(MemoStream);   // 将图片保存到内存流中
        Image1.Picture.Graphic.SaveToStream(MemoStream);
        MemoStream.Position := 0;
        scMemo1.Lines.LoadFromStream(MemoStream);
        FDQuery1.Close;
        FDQuery1.SQL.Clear;
        FDQuery1.SQL.Add('Insert into bVIP_Member (NameBM,TouX) values (:NameBM,:Photo)');
        FDQuery1.Params.ParamByName('NameBM').Value := editId.Text;
        FDQuery1.Params.ParamByName('Photo').LoadFromStream(MemoStream, ftBlob); // 读取保存的内存图    ftBlob
        FDQuery1.ExecSQL;
        Caption := '保存成功。';
    finally
        Jpg.Free;
        MemoStream.Free;     // 释放内存流
    end;
 //或者  TBlobField(FDquery1.FieldbyName('Field').LoadFromSteam(MemoStream));
end;

procedure TForm1.scButton1Click(Sender: TObject);
begin
    cxGrid1DBTableView1.DataController.RefreshExternalData;
end;

procedure TForm1.edt_JPG_AddRightButtonClick(Sender: TObject);
begin
    OpenPictureDialog1.Title := '打开图片';
    if OpenPictureDialog1.Execute then
    begin
        Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
        edt_JPG_Add.Text := OpenPictureDialog1.FileName;
    end;
end;

end.


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.042249917984009 seconds