2

我想创建一个类来查看任何格式的图像。图像存储在 blob 字段中。

我创造:

 const
   ImageType:array [0..6] of TGraphicClass=(TJpegimage, TBitmap, TWICImage,
                                            TGIFImage, TPNGImage, TMetafile,TIcon);

procedure tImageView.LoadFromStream(AStrm: TStream);
var
  Ext: string;
  NewGraphic: TGraphic;
  GraphicClass: TGraphicClass;

  i:integer;
  IsLoaded:boolean;
begin
   IsLoaded:=false;
   for i:=0 to Length(ImageType)-1 do begin
      try
         GraphicClass:=ImageType[i];
         NewGraphic := GraphicClass.Create;
         if Astrm.Size<>0 then begin
            NewGraphic.LoadFromStream(Astrm);
            IsLoaded:=true;
            Break;
            end;
      except
         try
            NewGraphic.Free;
         finally

            end;
         end;
      end;
   if Isloaded then begin
      fImg.Picture.Assign(NewGraphic);
      NewGraphic.Free;
      end
   else begin
      raise EInvalidGraphic.Create('Unknow format image!!!!!!!');
      end;
   end;

如果“ImageType”中的第一种类型的图像不正确,则正确类型下一个索引运行异常。为什么?我必须调用一些初始化程序吗?

4

0 回答 0