【问题标题】:Timage gets white and bitmap has size 0 bytesTimage 变白,位图大小为 0 字节
【发布时间】:2015-08-04 08:39:42
【问题描述】:

我的 TImage 组件中存储了一些数据。我想将图像保存为位图。执行此代码,TImage 内容变为白色,并且在硬盘上仅创建了一个 0 字节的 bmp 文件。我的代码有什么问题?

  MainStatusbar.SimpleText := 'save the image .... ';
  if SaveDialog.Execute then
  begin
    Image1.picture.Bitmap.SaveToFile(SaveDialog.filename);
  end;
  MainStatusbar.SimpleText := 'done ';

【问题讨论】:

  • 您的图像要么是空的,要么它的图片不是位图。请改用Image1.Picture.SaveToFile(SaveDialog.FileName)

标签: delphi bitmap


【解决方案1】:

TPicture 是多个不同类型图像的容器。如果当前图像不是TBitmap,则Picture.Bitmap 将不包含您的图像。当您引用Picture.Bitmap 时,您的原始图像将被破坏,并创建一个空的TBitmap。因此,当调用SaveToFile() 时,Picture.Bitmap 的明显解释为空。

您应该通过在 Picture 对象上调用 SaveToFile 来保存图像:

Image1.Picture.SaveToFile(...);

【讨论】:

    【解决方案2】:

    如果原始图像不是位图,则调用Picture.Bitmap 将擦除内容并创建一个空位图。根据原始图片格式,自动转换(例如从图标)可能是可能的,但必须在 TImage 之外完成。

    【讨论】:

      【解决方案3】:

      如果TImage.Picture.Graphic 属性当前不包含TBitmap,则访问TImage.Picture.Bitmap 属性将释放当前Graphic 并将其替换为空白TBitmap。这是记录在案的行为。

      既然要保存位图,请检查当前的Graphic 是否已经是TBitmap。如果是这样,请按原样保存。否则创建一个临时的TBitmap,将当前的Graphic 分配给它,然后保存它。

      MainStatusbar.SimpleText := 'save the image .... ';
      if SaveDialog.Execute then
      begin
        if Image1.Picture.Graphic is TBitmap then
          Image1.Picture.Bitmap.SaveToFile(SaveDialog.FileName)
        else
        begin
          Tmp := TBitmap.Create;
          try
            Tmp.Assign(Image1.Picture.Graphic);
            Tmp.SaveToFile(SaveDialog.FileName);
          finally
            Tmp.Free;
          end;
        end;
      end;
      MainStatusbar.SimpleText := 'done ';
      

      【讨论】:

        猜你喜欢
        • 2021-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多