如何将(Jpeg)图像保存到MySql并稍后检索? [英] How to save a (Jpeg) image to MySql and retrieve it later?

查看:153
本文介绍了如何将(Jpeg)图像保存到MySql并稍后检索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找代码示例,最好使用TADOConnection。

Looking for a code sample, preferably using TADOConnection.

我想将TImage的TPicture保存到MySql(最好是ODBC,而不仅仅是MySql)数据库后来我想要一个TImage,并将图片检索到它的TPicture属性。

I want to save the TPicture of a TImage to a MySql (preferably ODBC, rather than just MySql) database and later I want to crate a TImage and retrieve the picture to its TPicture property.

任何代码片段或链接到相同?

Any code snippets, or links to same?

推荐答案

可以使用BLOB字段。假设你有一个TAdoDataset实例,你想编辑一个图像字段。您可以使用这样的代码:

You can use BLOB fields. Suppose you have an instance of TAdoDataset, and you want to edit an image field. You can use a code like this:

  AStream := TMemoryStream.Create;
  try
    Image1.Picture.Graphic.SaveToStream(AStream);
    AStream.Position := 0;
    if ADODataSet1.Active then
    begin
      ADODataSet1.Edit;
      TBlobField(ADODataSet1.FieldByName('MyField')).LoadFromStream(AStream);
      ADODataSet1.Post;
    end;
  finally
    AStream.Free;
  end;

您还可以使用数据感知的DBImage控件,并将任何图像加载到其中意味着加载该图像进入该字段。

You can also use a DBImage control which is data-aware, and loading any image into it means loading that image into the field.

要检索字段数据,并将其加载到TImage实例中,可以使用以下代码:

To retrieve the field data, and load it into a TImage instance, you can have a code like this:

var
  AStream : TMemoryStream;
begin
  AStream := TMemoryStream.Create;
  try
    if ADODataSet1.Active then
    begin
      TBlobField(ADODataSet1.FieldByName('MyField')).SaveToStream(AStream);
      AStream.Position := 0;
      Image1.Picture.Graphic.LoadFromStream(AStream);
    end;
  finally
    AStream.Free;
  end;
end;

警告:在这段代码中,我假设Image1.Picture.Graphic不是零。如果您的图像控件为空,则Picture.Graphic属性为Nil,上面的代码将为您提供访问冲突。您有两个选择来避免这种情况:

Warning: In this code I assumed Image1.Picture.Graphic is not Nil. If your image control is empty, then Picture.Graphic property is Nil, and the code above will give you Access Violation. You have two options to avoid this:


  1. 不要将图像保存到
    内存流中,而是将其保存到本地
    临时文件,然后使用Image1.Picture.LoadFromFile加载图片

    LoadFromFile检查文件扩展名,
    ,根据文件扩展名,

    TGraphics后代类中的一个创建一个正确的对象。对于
    示例,如果文件是JPEG,则
    创建一个TJPEGImage
    类的实例,并将数据加载到其中。

  2. 您自己创建正确的
    对象并将其分配给
    Image1.Picture.Graphic。例如,
    如果您的图像是JPEG,创建一个
    实例的TJPEGImage,加载
    流,然后将其分配给
    Image1.Picture.Graphic。

如果要使用SQL命令(INSERT / UPDATE)插入或更新DB记录,则必须在SQL命令中使用SQL参数,然后您可以将流加载到代表您的图像字段的参数中:

If you are inserting or updating a DB record using SQL commands (INSERT/UPDATE), you have to use SQL parameters in your SQL command, then you can load the stream into the parameter which represents your image field:

///Sample SQL Command:

INSERT INTO My_Table_Name
(MyField1, MyField2) 
VALUES (:prmMyField1, :prmMyField2)


/// Sending INSERT command to DB server

var
  AStream : TMemoryStream;
begin
  AStream := TMemoryStream.Create;
  try
    Image1.Picture.Graphic.SaveToStream(AStream);
    AStream.Position := 0;
     // Save some random data into the first param
    ADOCommand1.Parameters.ParamByName('prmMyField1').Value := 1;
    // Save image stream into the second param
    ADOCommand1.Parameters.ParamByName('prmMyField2').LoadFromStream(AStream);
    ADOCommand1.Execute;
  finally
    AStream.Free;
  end;
end;

这篇关于如何将(Jpeg)图像保存到MySql并稍后检索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆