Delphi generates a QR code [PaintBox] and saves it as a local file and adds it to the [Image] control

RAD Studio 10.3 test √

Controls needed: scEdit, scGPButton, Image, PaintBox
Insert picture description here
need to declare : DelphiZXIngQRCode
download address: https://pan.baidu.com/s/1dN_7WGAlOIGoSdJJviQoEQ
extraction code: 8m9c
[At the same time, remember to add the path to search it, no matter what That way]


  private
    QRCodeBitmap: TBitmap;
  public
    procedure Update;

OnCreate event and OnDestroy event of the form :

procedure TForm1.FormCreate(Sender: TObject);                                 // 窗体创建
begin
  QRCodeBitmap := TBitmap.Create;                                             // 代码图位
  Update;
end;

procedure TForm1.FormDestroy(Sender: TObject);                                // 窗体销毁
begin
  QRCodeBitmap.Free;
end;

OnPaint event of PaintBox :

procedure TForm1.PaintBox1Paint(Sender: TObject);
var
  Scale: Double;
begin
  PaintBox1.Canvas.Brush.Color := clRed;                                      // 背景颜色
  PaintBox1.Canvas.FillRect(Rect(0, 0, PaintBox1.Height, PaintBox1.Width));   // 填充矩形
  if ((PaintBox1.Width > 0) and (PaintBox1.Height > 0)) then
  begin
    if PaintBox1.Width < PaintBox1.Height then
    begin
      Scale := PaintBox1.Width / QRCodeBitmap.Width;
    end
    else
    begin
      Scale := PaintBox1.Height / QRCodeBitmap.Height;
    end;
    { StretchDraw(Rect, Jpg); }
    { Rect:是你要画的区域(你要放大就设置的大一些,缩小就设置的小一些) }
    { Jpg:是要画的东西(我这里只是用Jpg文件做例子) }
    PaintBox1.Canvas.StretchDraw(Rect(0, 0, Trunc(Scale * QRCodeBitmap.Width), Trunc(Scale * QRCodeBitmap.Height)), QRCodeBitmap);
  end;
end;

The Updata method declared by itself :

procedure TForm1.Update;
var
  QRCode: TDelphiZXingQRCode;
  Column, Row: Integer;
begin
  QRCode := TDelphiZXingQRCode.Create;
  try
    QRCode.Data := scEdit1.Text;                                              // 数据
    QRCode.Encoding := TQRCodeEncoding(5);                                    // 编码方式 【qrUTF8BOM】
    QRCode.QuietZone := StrToIntDef(scEdit2.Text, 1);                         // 边框区域
    QRCodeBitmap.SetSize(QRCode.Rows, QRCode.Columns);                        // 设定大小
    {【Row:行  ;  Column:列】}
    for Row := 0 to QRCode.Rows - 1 do                                        // 循环行
    begin
      for Column := 0 to QRCode.Columns - 1 do                                // 循环列
      begin
        if (QRCode.IsBlack[Row, Column]) then                                 // 判断二维码次坐标是否是黑色的
        begin
          QRCodeBitmap.Canvas.Pixels[Column, Row] := clGreen;                 // 是的就给画布画上绿色
        end
        else
        begin
          QRCodeBitmap.Canvas.Pixels[Column, Row] := clWhite;                 // 否则就画白色
        end;
      end;
    end;
  finally
    QRCode.Free;
  end;
  PaintBox1.Repaint;                                                          // 重涂
end;

As far as the above code is concerned, the QR code can be generated, and the Update method can be added to the OnChange event of scEdit

procedure TForm1.scEdit1Change(Sender: TObject);
begin
  Update;
end;

The following is to save the contents of the PaintBox to the local and add to the Image control

procedure SaveBmpFromPaintBox(pbox: TPaintBox; fn: string);
var
  bmp: TBitmap;
  r: TRect;
begin
  bmp := TBitmap.Create;                                                      // 创建图位
  bmp.Width := pbox.Width;                                                    // 图位宽 = 盒子宽度
  bmp.Height := pbox.Height;                                                  // 图位高 = 盒子高度
  try
    r := Rect(0, 0, pbox.Height, pbox.Width);                                 // 矩形图的 左上右下 四边
    { 如果图象一【r1】的选取区域小于图象二【r2】中的选取区域,那么图一【r1】选取区域中的图象,
    拉伸填充到图象二【r2】中的选取区域。(图象区域相同的复制不会造成图象失真,如果变大或者变小,就容易造成失真) }
    bmp.Canvas.CopyRect(r, pbox.Canvas, r); { 两个 r 在上面分别用【r1、r2】表示(从左至右),中间参数为拷贝内容 }
    bmp.SaveToFile(fn);                                                       // 文件保存地址
  finally
    FreeAndNil(bmp);
  end;
end;

procedure TForm1.scGPButton2Click(Sender: TObject);
begin
  SaveBmpFromPaintBox(paintbox1, 'd:\test.bmp');                              // 保存到本地 【需要改成jpg或png的直接改文件的后缀即可】
  Image1.Picture.LoadFromFile('d:\test.bmp');                                 // 本地图片展示到 Image 上
end;

I have a question here. I want to ask you guys
why the following warning appears. Is there any way to solve this warning?

Insert picture description here


Note source code


A little bit of notes recorded during study, so that you can read it later.

Guess you like

Origin blog.csdn.net/qq_44111597/article/details/108359774