【问题标题】:How to transfer a scanned image from WIA ImageFile to TBitmap without saving it to disk?如何将扫描的图像从 WIA ImageFile 传输到 TBitmap 而不将其保存到磁盘?
【发布时间】:2012-10-28 12:19:36
【问题描述】:

我正在使用此代码从 WIA 获取扫描图像:

const
  wiaFormatJPEG = '{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}';
  wiaFormatPNG = '{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}';
var
  CommonDialog: ICommonDialog;
  AImage: IImageFile;
  i: Integer;
begin
  CommonDialog := CreateOleObject('WIA.CommonDialog') as ICommonDialog;

  for i := 1 to Scanner.Properties.Count do
  begin
    if (Scanner.Properties[i].Name = 'Horizontal Resolution') or
      (Scanner.Properties[i].Name = 'Vertical Resolution') then
      Scanner.Properties[i].Set_Value(72)
    else if Scanner.Properties[i].Name = 'Horizontal Extent' then
      Scanner.Properties[i].Set_Value(Round(8.27 * 72))
    else if Scanner.Properties[i].Name = 'Vertical Extent' then
      Scanner.Properties[i].Set_Value(Round(11.00 * 72));
  end;
  AImage := IUnknown(CommonDialog.ShowTransfer(Scanner, wiaFormatPNG, True)) as IImageFile;
  //Save the image
  AImage.SaveFile('D:\1.' + AImage.FileExtension);
  imgImage.Picture.LoadFromFile('D:\1.' + AImage.FileExtension);
  DeleteFile('D:\1.' + AImage.FileExtension);
end;

Scanner 使用以下代码初始化:

Scanner := DevMgr.DeviceInfos[Integer(cbWIASource.Items.Objects[cbWIASource.ItemIndex])].Connect.Items[1];

并且DevMgrcbWIASource 使用以下代码进行初始化:

DevMgr := CreateOleObject('WIA.DeviceManager') as IDeviceManager;
for i := 1 to DevMgr.DeviceInfos.Count do
    for j := 1 to DevMgr.DeviceInfos[i].Properties.Count do
      if DevMgr.DeviceInfos[i].Properties[j].Name = 'Name' then
      begin
        cbWIASource.Items.AddObject(DevMgr.DeviceInfos[i].Properties[j].Get_Value, TObject(i));
        Break;
      end;

我想知道是否有一种方法可以在不先将其保存到磁盘的情况下复制扫描的文档。我在 MSDN 上读到我可以访问 ImageFile 的成员 ARGBData 以访问像素数据,但是有没有一种简单的方法可以将整个图像从 FileData 复制到 TBitmap?例如,我可以使用TMemoryStream吗?


作为更新,我在 MSDN 上找到了this example。我对 VB 一无所知,但我猜Picture 对象是HBITMAP 的包装器。那么,得出ImageFile.Picture 属性是我需要的属性是否合乎逻辑?

【问题讨论】:

  • IImageFile 真正叫什么? Websearch 没听说过。
  • 好的,所以它的真名是ImageFile
  • 创建一个 32bpp 像素格式的TBitmap。然后使用位图的ScanLine 属性从ARGBData 跨像素数据传输。
  • @David,我认为这还不够。 ARGBData 属性返回按 ARGB 顺序排列的字节集合,而 32 位 Delphi 位图使用 BGRA 顺序。

标签: delphi delphi-xe2 wia


【解决方案1】:

IImageFile 有一个属性 FileData 提供对二进制图像数据的访问,通过IVector.BinaryData

【讨论】:

    猜你喜欢
    • 2021-02-27
    • 2021-12-29
    • 2022-10-19
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    相关资源
    最近更新 更多