-2

我正在使用paintbox组件使用矩形、多边形和其他画布方法绘制各种形状。用户创建绘图后,我想保存位图以在列表框中使用。问题是绘图可能只使用画布的一小部分,并且列表框中的结果图像会非常小,除非我通过仅选择油漆框原始画布的已使用部分来调整其大小。所以问题是我如何确定画布的哪一部分已被使用,以便我只能提取画布的那部分以加载到位图中以在列表框中显示?

(注意:我在上面进行了编辑以澄清问题)

实际程序有一个画框 (200x200) 和一个图像 (32 x 32)。图像使用Bitmap1.Canvas.CopyRect(Dest, PaintBox1.Canvas, Source);. 如果在 200x200 的paintbox.canvas 中,paintbox 中的绘图只有 20x20,那么在 Image.canvas 中生成的位图在 32x32 的 image.canvas 中将非常小。我需要放大它,这意味着我必须确定颜料盒中使用区域的实际大小并更改“CopyRec”中的源大小。

4

1 回答 1

0

我制定的一种方法是基于以下假设:已绘制的各种项目(例如圆形、矩形、文本等)都放置在中性背景上。在这种情况下,我可以读取位图,tbitmap.scanline用于比较绘图的颜色与背景颜色,并计算每行中的绘图范围以确定整个位图中的绘图范围。

  TRGBTriple = packed record
    rgbtBlue: Byte;
    rgbtGreen: Byte;
    rgbtRed: Byte;
  end;
  TRGBTripleArray = ARRAY[Word] of TRGBTriple;
  pRGBTripleArray = ^TRGBTripleArray; // use a PByteArray for pf8bit color

function findBMPExtents (Bmp : tbitmap; BkgdClr : longint):trect;
// finds the extents of an image in a background or BkgdClr color
//works on 24 bit colors
var
  P : pRGBTripleArray;
  x,y : integer;
  tfound, bfound, done : boolean;
  WorkTrpl : TRGBTriple;
  WorkRect : trect;
begin
  result.top := 0;
  result.bottom := Bmp.height;
  result.left := Bmp.Width;
  result.right := 0;
  tfound := false;
  bfound := false;
  WorkTrpl := getRGB (BkgdClr);

  //find left and top
  y := 0;
  done := false;
  Repeat
    P := Bmp.ScanLine[y];
    x := 0;
    Repeat
      if (p[x].rgbtBlue <> WorkTrpl.rgbtBlue) or
         (p[x].rgbtGreen <> WorkTrpl.rgbtGreen) or
         (p[x].rgbtRed <> WorkTrpl.rgbtRed) then
        begin
          tfound := true;
          if x <= result.left then begin
            result.left := x;
            done := true;
          end;
        end;
      inc (x);
    until (x = bmp.width) or done;
    done := false;
    inc (y);
    if not tfound then
      inc(result.top);
  until (y = bmp.height);

  //find right and bottom
  y := bmp.height - 1;
  done := false;
  Repeat
    P := Bmp.ScanLine[y];
    x := bmp.width-1;
    Repeat
      if (p[x].rgbtBlue <> WorkTrpl.rgbtBlue) or
         (p[x].rgbtGreen <> WorkTrpl.rgbtGreen) or
         (p[x].rgbtRed <> WorkTrpl.rgbtRed) then
        begin
          bfound := true;
          if x >= result.right then begin
            result.right := x;
            done := true;
          end;
        end;
      dec (x);
    Until (x = 0) or done;
    if not bfound then
      dec(result.bottom);
    done := false;
    dec (y);
  Until (y = -1);
  dec(result.bottom);
end;
于 2018-03-09T18:26:38.773 回答