gpt4 book ai didi

mysql - 如何将存储在 MySQL 表中的位图图像转换为 JPEG 格式?

转载 作者:行者123 更新时间:2023-12-01 00:52:59 30 4
gpt4 key购买 nike

我有一个存储位图图像的 MySQL 表,我想将它们转换为 JPEG 格式到同一个表中。谁能帮我找到解决方案?

我需要这个来减小表的大小...

最佳答案

当您使用 ADO 访问 MySQL 数据库时,它可能看起来像这样(未经测试)。此代码假定您有一个要使用的表,名称为 YourTable,您要将图像从中转换为 ImageField 的 BLOB 字段。请注意,您必须在 ADOConnection 对象的 ConnectionString 属性中指定数据库的连接字符串:

uses
DB, ADODB, JPEG;

procedure ConvertImage(BlobField: TBlobField);
var
BMPImage: TBitmap;
JPEGImage: TJPEGImage;
MemoryStream: TMemoryStream;
begin
MemoryStream := TMemoryStream.Create;
try
BlobField.SaveToStream(MemoryStream);
BMPImage := TBitmap.Create;
try
MemoryStream.Position := 0;
BMPImage.LoadFromStream(MemoryStream);
JPEGImage := TJPEGImage.Create;
try
JPEGImage.Assign(BMPImage);
MemoryStream.Position := 0;
JPEGImage.SaveToStream(MemoryStream);
finally
JPEGImage.Free;
end;
finally
BMPImage.Free;
end;
MemoryStream.Position := 0;
BlobField.LoadFromStream(MemoryStream);
finally
MemoryStream.Free;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
ADOTable: TADOTable;
ADOConnection: TADOConnection;
begin
ADOConnection := TADOConnection.Create(nil);
try
ADOConnection.LoginPrompt := False;
// here you have to specify the connection string to your database
// according to your connection parameters
ADOConnection.ConnectionString := '<enter your connection string here>';
ADOConnection.Open;
if ADOConnection.Connected then
begin
ADOTable := TADOTable.Create(nil);
try
ADOTable.Connection := ADOConnection;
ADOTable.TableName := 'YourTable';
ADOTable.Filter := 'ImageField IS NOT NULL';
ADOTable.Filtered := True;
ADOTable.CursorType := ctOpenForwardOnly;
ADOTable.Open;
ADOTable.First;
while not ADOTable.Eof do
begin
ADOTable.Edit;
ConvertImage(TBlobField(ADOTable.FieldByName('ImageField')));
ADOTable.Post;
ADOTable.Next;
end;
finally
ADOTable.Free;
end;
end;
finally
ADOConnection.Free;
end;
end;

关于mysql - 如何将存储在 MySQL 表中的位图图像转换为 JPEG 格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13632959/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com