我有一个存储位图图像的 MySQL 表,我想将它们转换为 JPEG 格式到同一个表。谁能帮我找到解决方案?
我需要这个来减小桌子的大小......
我有一个存储位图图像的 MySQL 表,我想将它们转换为 JPEG 格式到同一个表。谁能帮我找到解决方案?
我需要这个来减小桌子的大小......
当您使用 ADO 访问您的 MySQL 数据库时,它可能看起来像这样(它未经测试)。此代码假定您拥有要使用的名为 as 的表YourTable
和要从 as 转换图像的 BLOB 字段ImageField
。ConnectionString
请注意,您必须在对象的属性中指定到数据库的连接字符串ADOConnection
:
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;