You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Delphi中JPG转Bitmap报错:TPersistent与TFileName类型不兼容

问题解决与像素化功能实现

错误原因分析

  1. Incompatible types: 'TPersistent' and 'TFileName':TBitmap.Assign()方法仅接收TPersistent类型对象(如另一个TBitmap、TJPEGImage),直接传入字符串文件名属于类型不匹配,触发该错误。
  2. Bitmap image is not valid:TBitmap.LoadFromFile()仅支持BMP格式,直接加载JPG/PNG等非BMP文件时,会因格式不兼容抛出此错误。

关键修复与功能实现步骤

  • 移除全局OpenDialog变量,改用局部变量并确保使用后释放,避免内存泄漏。
  • 借助TPicture加载任意格式图片,再将内容赋值给TBitmap,解决跨格式转换问题。
  • 实现真实像素化效果:先将图像缩小到低分辨率(如10x10),再放大回原始尺寸,通过低分辨率拉伸生成像素块效果。

修正后的完整代码

unit demo_2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Imaging.Jpeg;

type
  TForm1 = class(TForm)
    btn1_select_img: TButton;
    btn2_pixellate: TButton; // 按钮重命名,功能更贴合
    lbl_selected_file: TLabel;
    lbl_output_path: TLabel;
    img1: TImage;
    pnl_img: TPanel;
    pnl_btns: TPanel;
    procedure btn1_select_imgClick(Sender: TObject);
    procedure btn2_pixellateClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1_select_imgClick(Sender: TObject);
var
  OpenDialog: TOpenDialog;
begin
  OpenDialog := TOpenDialog.Create(nil);
  try
    OpenDialog.Filter := 'Image Files|*.bmp;*.jpg;*.jpeg;*.png|All Files|*.*';
    OpenDialog.Options := [ofPathMustExist, ofFileMustExist];
    if OpenDialog.Execute then
    begin
      lbl_selected_file.Caption := OpenDialog.FileName;
      img1.Picture.LoadFromFile(OpenDialog.FileName);
    end;
  finally
    OpenDialog.Free; // 确保释放资源,避免内存泄漏
  end;
end;

procedure TForm1.btn2_pixellateClick(Sender: TObject);
var
  PixellatedImage: TBitmap;
  TempBitmap: TBitmap;
  OrigWidth, OrigHeight: Integer;
begin
  // 检查是否已选择图片,避免空操作报错
  if img1.Picture.Graphic = nil then Exit;

  OrigWidth := img1.Picture.Graphic.Width;
  OrigHeight := img1.Picture.Graphic.Height;

  PixellatedImage := TBitmap.Create;
  TempBitmap := TBitmap.Create;
  try
    // 将原始图像导入临时Bitmap
    TempBitmap.Assign(img1.Picture.Graphic);
    // 缩小到低分辨率,生成像素化基础
    TempBitmap.Width := 10;
    TempBitmap.Height := 10;
    // 放大回原始尺寸,拉伸生成像素块效果
    PixellatedImage.Width := OrigWidth;
    PixellatedImage.Height := OrigHeight;
    PixellatedImage.Canvas.StretchDraw(Rect(0, 0, OrigWidth, OrigHeight), TempBitmap);

    // 展示处理后的图像
    img1.Picture.Assign(PixellatedImage);
  finally
    TempBitmap.Free;
    PixellatedImage.Free;
  end;
end;

end.

额外优化说明

  • 优化文件选择对话框的过滤规则,仅显示常见图像格式,提升用户操作体验。
  • 增加图片选择状态判断,避免未选图时触发无效操作。
  • 重命名按钮为btn2_pixellate,功能指向更清晰。

内容的提问来源于stack exchange,提问作者Omar Gonzales

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 13:35:17