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.

相关阅读 >>

Delphi xe2 - 实现无敌关机键

Delphi2010 图片格式转换bmp, png,jpeg, gif, tiff , wmphoto

Delphi下firedac连接mysql数据库零起点

Delphi 新版内存表 fdmemtable

Delphi xe5 android 发短信以及目录

Delphi 拷贝文件时有进度显示

Delphi 通过wmi获取u盘硬件特征码

Delphi tstringlist.find

Delphi捕捉屏幕

Delphi 日期相关总结20190702完结篇

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...