我有一个将图像加载到 Zimage(graphcontrol)中的应用程序。我遇到的一个问题是原始图像的质量比最终在组件中显示的图像质量要好得多。显示的那个看起来上面有随机的黑色标记。那我做什么:
- 将图像从文件夹加载到 TJpegimage
- 将 jpg 分配给位图
- 将位图保存到对象列表
重复上述操作,直到所有图像都加载到对象列表中
然后我创建一个位图
- 将对象列表中的第一个图像分配给它
- 在 Canvas 上绘制位图。
但它看起来比原来的差很多。对此有什么帮助或想法吗?
这是加载代码。
procedure TForm1.LoadImages(const Dir: string);
var
i: Integer;
CurFileName: string;
JpgIn: TJPEGImage;
BmpOut: TBitmap;
begin
//set up progress bar
progressbar1.Min := 0;
progressbar1.Max := GetFilesCount(dir,'*.*');
Label3.Visible := true;
progressbar1.Visible := true;
//sets array for length
SetLength(hwArray,GetFilesCount(dir,'*.*'));
//sets index for object list
CurIdx := -1;
i := 0;
while True do
begin
//gets file name out of current folder
CurFileName := Format('%s%d.jpg',
[IncludeTrailingPathDelimiter(Dir), i]);
if not FileExists(CurFileName) then
Break;
//count files in folder for progress bar.
progressbar1.StepBy(1);
//creates jpgin
JpgIn := TJPEGImage.Create;
try
//loads jpgin with file
JpgIn.LoadFromFile(CurFileName);
//creates TBitmap and sets width to same as jpgs
BmpOut := TBitmap.Create;
bmpout.Width := jpgin.Width;
bmpout.Height := jpgin.Height;
try
BmpOut.Assign(JpgIn);
//adds 1 to index for object list. thus starting at 0
curIdx := curIdx+1;
//add bitmap to objectlist
CurIdx:= mylist.Add(TBitmap(bmpout));
hwArray[CurIdx][0]:=jpgin.Width;
hwArray[CurIdx][1]:=jpgin.height;
finally
end;
finally
JpgIn.Free;
end;
Inc(i);
end;
//makes sure cout is above 0 thus files added
if mylist.Count > 0 then
begin
try
CurIdx := 0;
getBitmapfromList(CurIdx,bmpout);
ZImage1.Bitmap.Assign(bmpout);
//image1.Canvas.Assign(bmpout);
finally
BmpOut.Free;
end;
end;
Label3.Visible := false;
progressbar1.Visible := false;
page:= '0';
zimage1.DblClick;
end;
函数从列表中获取位图
procedure Tform1.getBitmapfromList(index:integer;var Bitm:TBitmap);
begin
Bitm.Assign(TBitmap(mylist[index]));
if (Bitm.Width<>hwArray[index][0]) OR (Bitm.Height<>hwArray[index][1]) then begin
ShowMessage('Size differs');
Bitm.Width:=hwArray[index][0];
Bitm.Height:=hwArray[index][1];
end;
end;
“下一个”按钮可查看下一个图像
procedure TForm1.Button3Click(Sender: TObject);
var
BmpOut: TBitmap;
begin
bmpout := TBitmap.Create;
CurIdx:= strtoint(page);
getBitmapfromList(CurIdx,bmpout);
ZImage1.Bitmap.Assign(bmpout);
//image1.Canvas.Assign(bmpout);
page := inttostr(strtoint(page) +1); //this should show next item in ilst?
zimage1.Repaint;
end;
感谢您的帮助或建议。
以下是图片:
http://s7.postimage.org/48mectvsp/image_Difference.png
编辑
似乎一旦我放大图像,它就会变得更好。最差的质量是当它全屏/完全缩放时..原始尺寸。
最佳答案
- a) 我假设所有图像都有不同的尺寸。如果显示的图像“ZImage1”应始终具有相同的高度或宽度(两者之一,或者它是用拉伸(stretch)配合==低质量制成的),则必须确定比率(orginal.height/original.width)。然后必须在新关系中计算“ZImage1”(高度或宽度)。
- b) 设置 ZImage1.AutoSize:=false;
c) 设置 ZImage1.Stretch:=false;
d) bmpout.Width 和 Height 必须设置为当前
mylist[CurIdx]
大小。
bmpout.Assign(TBitmap(mylist[CurIdx])); Zimage1.Bitmap.Width := bmpout.Width; Zimage1.Bitmap.Height := bmpout.Height; ZImage1.Bitmap.Assign(bmpout);
注意:
您没有*分配*到mylist ..您存储它 CurIdx:= mylist.Add(TBitmap(bmpout));
我看不到你如何创建“mylist”。
也许这就是原因,如果你想用bmpout.Assign(TBitmap(mylist[CurIdx]));
返回它,也许没有自动设置bmpout。宽度和 bmpout.Height。
提示:
创建一个额外的数组来存储高度和宽度。
....
JpgIn.LoadFromFile(CurFileName);
BmpOut := TBitmap.Create;
bmpout.Width := jpgin.Width;
bmpout.Height := jpgin.Height;
....
CurIdx:= mylist.Add(TBitmap(bmpout));
hwArray[CurIdx][0]:=jpgin.Width;
hwArray[CurIdx][1]:=jpgin.height;
....
制定一个程序 编辑:对其进行测试;
procedure getBitmapfromList(index:integer;var Bitm:TBitmap);
begin
Bitm.Assign(TBitmap(mylist[index]));
if (Bitm.Width<>hwArray[index][0]) OR (Bitm.Height<>hwArray[index][1]) then begin
ShowMessage('Size differs');
Bitm.Width:=hwArray[index][0];
Bitm.Height:=hwArray[index][1];
end;
end;
调用方式:
....
getBitmapfromList(CurIdx,bmpout);
....
ZImage1.Bitmap.Assign(bmpout);
....
编辑:创建hwArray
type
Tarray2size = Array[0..1] of integer;
....
var
hwArray : Array of Tarray2size;
....
procedure TForm1.LoadImages(const Dir: string);
....
begin
//set up progress bar
progressbar1.Min := 0;
showmessage(inttostr(GetFilesCount(dir,'*.*')));
SetLength(hwArray,GetFilesCount(dir,'*.*'));
....
关于delphi - 与原始图像相比,加载到图像控件后图像质量较差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11043801/