Delphi 如何将Timage位图正确分配给jpg?
我正在执行以下操作来调整Timage图形的大小并将其另存为jpg。图像保存为空白 下面是我分配时间的方式Delphi 如何将Timage位图正确分配给jpg?,delphi,delphi-10-seattle,Delphi,Delphi 10 Seattle,我正在执行以下操作来调整Timage图形的大小并将其另存为jpg。图像保存为空白 下面是我分配时间的方式 begin with TOpenDialog.Create(self) do try Caption := 'Open Image'; Options := [ofPathMustExist, ofFileMustExist]; Filter := 'JPEG Image File (*.jpg)|*.jpg|JPEG Image File (
begin
with TOpenDialog.Create(self) do
try
Caption := 'Open Image';
Options := [ofPathMustExist, ofFileMustExist];
Filter := 'JPEG Image File (*.jpg)|*.jpg|JPEG Image File (*.jpeg)|*.jpeg|Bitmaps (*.bmp)|*.bmp';
if Execute then
image1.Picture.LoadFromFile(FileName);
finally
Free;
end;
end;
下面是我如何调整timage的大小并将其分配到jpg中
with Image2.Canvas do begin
Lock;
try
try
Image2.Picture.Graphic.Width := 50;
Image2.Picture.Graphic.Height := 50;
FillRect(ClipRect);
StretchDraw(rect(0, 0, 50, 50), image1.Picture.Bitmap);
except
on E: Exception do
//log
end;
finally
Unlock;
end;
end;
Jpg := TJPEGImage.Create;
try
Jpg.Performance := jpBestSpeed;
Jpg.ProgressiveEncoding := True;
Jpg.ProgressiveDisplay := True;
Jpg.Assign(image2.Picture.Bitmap);
Jpg.CompressionQuality := 90;
Jpg.Compress;
jpg.SaveToFile('screena.jpg');
finally
Jpg.Free;
end;
保存的jpg变为空白我做错了什么?我所需要做的就是将图像从磁盘加载到Timage中,然后调整宽度和高度,并将其重新分配到jpg中。图像为空的原因是,在将
Image1
绘制到Image2
的画布上时使用了Picture.Bitmap
属性:
StretchDraw(rect(0, 0, 50, 50), image1.Picture.Bitmap);
如果尚未将.bmp
文件加载到Image1
中,访问位图
属性将擦除当前图像并将其替换为空白图像。这是:
当图片对象包含位图时,使用Bitmap
引用图片对象。如果在图片包含图元文件或图标图形时引用了位图
,图形将不会被转换()。相反,图片的原始内容将被丢弃,位图
返回一个新的空白位图
该方法接受其第三个参数中的任何TGraphic
对象。您应该传入Picture.Graphic
属性,而不是Picture.Bitmap
属性:
StretchDraw(rect(0, 0, 50, 50), image1.Picture.Graphic);
一个旁注:在分配<代码> TopEndial.Faster 属性时,考虑使用VCL的函数在<代码>图形< /代码>单元:
返回与“打开”或“保存”对话框的filter
属性兼容的文件筛选器
调用GraphicFilter
获取打开的、打开的图片、保存图片或保存对话框的Filter
属性的值。graphicsclass
参数可以指定以下值之一:TBitmap
,TGraphic
,TIcon
,TMetafile
,或TGraphic
的用户定义后代
例如:
Filter := GraphicFilter(TGraphic);
如果您要手动填充过滤器
,那么至少对于不同的JPEG扩展名没有单独的条目。将它们组合在一个条目中,例如:
Filter := 'JPEG Images (*.jpg, *.jpeg)|*.JPG;*.JPEG|Bitmap Images (*.bmp)|*.BMP';
你为什么要用TImage?这是一个GUI控件。可能是因为图像正在显示给用户。使用
TOpenDialog
意味着正在使用UI。在任何情况下,TImage2
都不是真正需要的,它可以用一个简单的TBitmap
代替:bmp:=TBitmap.Create;尝试bmp。宽度:=50;高度:=50;StretchDraw(rect(0,0,50,50),image1.Picture.Graphic)。。。赋值(bmp)。。。最后是bmp.Free;结束代码>