加入图片 - Delphi

时间:2015-08-10 23:27:12

标签: delphi delphi-xe5

如何使用Delphi合并两个图像。我想过使用CopyRect但是无法实现它。如何使用矩形形状的位图附加JPG图像。我需要将图像置于矩形中心,怎么做?

enter image description here

procedure TForm1.Button1Click(Sender: TObject);
var
  bmp, bmp1: TBitmap;
  jpg: TJpegImage;
  scale: Double;
begin

  if opendialog1.execute then
  begin
    jpg := TJpegImage.Create;
    try
      jpg.Loadfromfile(opendialog1.filename);
      if jpg.Height > jpg.Width then
        scale := 98 / jpg.Height
      else
        scale := 98 / jpg.Width;
      bmp := TBitmap.Create;
      try
        {Create thumbnail bitmap, keep pictures aspect ratio}
        bmp.Width := Round(jpg.Width * scale);
        bmp.Height:= Round(jpg.Height * scale);

        //BPM1
         bmp1 := TBitmap.Create;
         bmp1.SetSize(98, 98);
         bmp1.Canvas.Brush.Color := RGB(243,243,243);
         bmp1.Canvas.Pen.Style:= psClear;
         bmp1.Canvas.Rectangle(0, 0, 98, 98);

        bmp.Canvas.StretchDraw(bmp.Canvas.Cliprect, jpg);
        {Draw thumbnail as control}
        //Juntar os 2
        self.Canvas.Draw(10, 10, bmp1);
        self.Canvas.Draw(10, 10, bmp);
        Logo.Picture.Assign(bmp);
        {Convert back to JPEG and save to file}
        jpg.Assign(bmp);
        jpg.SaveToFile(
          ChangeFileext(opendialog1.filename, '_thumb.JPG')
        );
      finally
        bmp.free;
        bmp1.free;
      end;
    finally
      jpg.free;
    end;
  end;
end;

2 个答案:

答案 0 :(得分:4)

您不应该使用bmp.Canvas.ClipRect作为StretchDraw()来电的目标矩形。指定所需的实际矩形,在本例中为bmp的完整尺寸。

如果您希望边框一直缩放图像,但保留最终图像的98x98尺寸,就像JPG显示的那样,那么您的比例需要基于小于98px。例如,要使边框至少宽10倍,请将您的比例缩小20px(每侧10px)。如果你不减小你的比例,缩放图像的宽度和/或高度将精确地为98px,这不是你的示例JPG所示。

当您在bmp之上绘制bmp1时,通过从bmp的尺寸中减去其尺寸并将结果分成两半来居中bmp1

请勿在表单Canvas事件之外的表格OnPaint上绘图。如果您希望表单显示图像,请使用TImage组件。

尝试更像这样的事情:

procedure TForm1.Button1Click(Sender: TObject);
var
  bmp, bmp1: TBitmap;
  jpg: TJPEGImage;
  scale: Double;
begin
  if OpenDialog1.Execute then
  begin
    jpg := TJPEGImage.Create;
    try
      jpg.LoadFromFile(OpenDialog1.FileName);
      if jpg.Height > jpg.Width then
        scale := 78 / jpg.Height
      else
        scale := 78 / jpg.Width;

      bmp := TBitmap.Create;
      try
        {Create thumbnail bitmap, keep pictures aspect ratio}
        bmp.SetSize(Round(jpg.Width * scale), Round(jpg.Height * scale));
        bmp.Canvas.StretchDraw(Rect(0, 0, bmp.Width, bmp.Height), jpg);

        //BPM1
        bmp1 := TBitmap.Create;
        try
          bmp1.SetSize(98, 98);
          bmp1.Canvas.Brush.Color := RGB(243, 243, 243);
          bmp1.Canvas.Pen.Style := psClear;
          bmp1.Canvas.Rectangle(0, 0, bmp1.Width, bmp1.Height);

          bmp1.Canvas.Draw((bmp1.Width - bmp.Width) div 2, (bmp1.Height - bmp.Height) div 2, bmp);

          {Draw thumbnail as control}
          //Juntar os 2
          Image1.Picture.Assign(bmp1);
        finally
          bmp1.free;
        end;

        Logo.Picture.Assign(bmp);
        {Convert back to JPEG and save to file}
        jpg.Assign(bmp);
        jpg.SaveToFile(ChangeFileExt(OpenDialog1.FileName, '_thumb.JPG'));
      finally
        bmp.free;
      end;
    finally
      jpg.free;
    end;
  end;
end;

答案 1 :(得分:1)

我一直在搅拌,直到我完成了他想要的东西。我唯一不能做的就是改变图像背景,我会放入空白的RGB(0,0,0),做了测试,但失败了。

enter image description here enter image description here enter image description here enter image description here enter image description here

procedure TForm1.Button2Click(Sender: TObject);
var
  bmp: TBitmap;
  jpg: TJPEGImage;
  scale: Double;
  widthL, HeightL, pt1, pt2, pt3, pt4: integer;
  verdd : boolean;
begin
  if OpenDialog1.Execute then
  begin
  try
          jpg := TJPEGImage.Create;
          verdd := false;
          try
            //Dimensões
            widthL := 98;
            HeightL := 98;
            jpg.LoadFromFile(OpenDialog1.FileName);
            if (jpg.Height >= jpg.Width) AND (HeightL <= jpg.Height) then begin
              scale := widthL / jpg.Height;
            end else if (jpg.Height <= jpg.Width) AND (widthL <=  jpg.Width) then begin
              scale := HeightL / jpg.Width;
            end else begin
              verdd := true;
            end;
                bmp := TBitmap.Create;
                try
                  {Create thumbnail bitmap, keep pictures aspect ratio}
                  bmp.SetSize( widthL,HeightL);
                  if not verdd then begin
                      pt1 := (widthL - Round(jpg.Width * scale)) div 2;
                      pt2 := (HeightL - Round(jpg.Height * scale)) div 2;
                      pt3 := Round(jpg.Width * scale) + pt1;
                      pt4 := Round(jpg.Height * scale) + pt2;
                      bmp.Canvas.StretchDraw(Rect(pt1, pt2, pt3, pt4), jpg);
                  end else begin
                      pt1 := (widthL - jpg.Width) div 2;
                      pt2 := (HeightL - jpg.Height) div 2;
                      pt3 := jpg.Width + pt1;
                      pt4 := jpg.Height + pt2;
                      bmp.Canvas.StretchDraw(Rect(pt1, pt2, pt3, pt4), jpg);
                  end;

                  Logo.Picture.Assign(bmp);
                  {Convert back to JPEG and save to file}
                  jpg.Assign(bmp);
                  jpg.SaveToFile(ChangeFileExt(OpenDialog1.FileName, '_thumb.JPG'));
                finally
                  bmp.free;
                end;
          finally
            jpg.free;
          end;
  except
       showMessage('Erro ao carregar imagem');    ///////////////////////////////////
  end;
  end;
end;