1

我在 ImgView32 的一层上画了一条虚线。稍后,我想将每一层保存为透明的 PNG。对于我拥有的任何其他图层,保存效果很好。但是对于绘图层,它没有。

为了使问题更易于理解,请从 gr32 库中获取示例代码,更具体地说是 Layers 示例。其主菜单中的选项之一是添加自定义绘图层(新建自定义层 -> 简单绘图层)。然后尝试将该图层保存为透明的 PNG 图像,您最终会得到一个损坏的 PNG 文件(您无法使用任何其他图片查看器打开它,例如 Paint.net 或 Microsoft Photo Viewer)。如果您尝试将图层的 bitmap32 保存为位图,则会发生同样的事情,如下面的代码所示......

我尝试了两种将 Bitmap32 保存为透明 PNG 的方法,所以第一种方法如下:

procedure TMainForm.SavePNGTransparentX(bm32:TBitmap32; dest:string);
var
  Y: Integer;
  X: Integer;
  Png: TPortableNetworkGraphic32;

  function IsBlack(Color32: TColor32): Boolean;
  begin
    Result:= (TColor32Entry(Color32).B = 0) and
             (TColor32Entry(Color32).G = 0) and
             (TColor32Entry(Color32).R = 0);
  end;

  function IsWhite(Color32: TColor32): Boolean;
  begin
    Result:= (TColor32Entry(Color32).B = 255) and
             (TColor32Entry(Color32).G = 255) and
             (TColor32Entry(Color32).R = 255);
  end;

begin
    bm32.ResetAlpha;
    for Y := 0 to bm32.Height-1 do
      for X := 0 to bm32.Width-1 do
      begin
//        if IsWhite(bm32.Pixel[X, Y]) then
//          bm32.Pixel[X,Y]:=Color32(255,255,255,  0);
        if IsBlack(bm32.Pixel[X, Y]) then
          bm32.Pixel[X,Y]:=Color32(  0,  0,  0,  0);
      end;

    Png:= TPortableNetworkGraphic32.Create;
    try
      Png.Assign(bm32);
      Png.SaveToFile(dest);
    finally
      Png.Free;
    end;

end;

因此,如果我像这样将 PNG 加载到图层中,则上述方法有效:

mypng := TPortableNetworkGraphic32.Create;
mypng.LoadFromStream(myStream);
B := TBitmapLayer.Create(ImgView.Layers);
with B do
   try
      mypng.AssignTo(B.Bitmap);
      ...

但是,一旦我尝试保存使用 Layers 示例中的代码创建的层,结果就会损坏。即使我尝试将图层保存为这样的位图(尽管这不是我的意图,因为我需要它们是 PNG):

mylay := TBitmapLayer(ImgView.Layers.Items[i]);
mylay.Bitmap.SaveToFile('C:\tmp\Layer'+IntToStr(i)+'.bmp');

发生同样的腐败。所以,这不像我收到异常或任何东西......它只是以某种方式被保存损坏;

我还尝试了其他方法将 Bitmap32 保存为透明 PNG,例如 GR32_PNG 方法:

function SaveBitmap32ToPNG (sourceBitmap: TBitmap32;transparent: Boolean;bgColor32: TColor32;filename: String;compressionLevel: TCompressionLevel = 9;interlaceMethod: TInterlaceMethod = imNone): boolean;
var  png: TPNGImage;
begin
  result := false;
  try
    png := Bitmap32ToPNG (sourceBitmap,false,transparent,WinColor(bgColor32),compressionLevel,interlaceMethod);
    try
      png.SaveToFile (filename);
      result := true;
    finally
      png.Free;
    end;
  except
    result := false;
  end;
end;

在哪里

function Bitmap32ToPNG (sourceBitmap: TBitmap32;paletted, transparent: Boolean;bgColor: TColor;compressionLevel: TCompressionLevel = 9;interlaceMethod: TInterlaceMethod = imNone): TPNGImage; // TPNGObject
var
  bm: TBitmap;
  png: TPNGImage;//TPngObject;
  TRNS: TCHUNKtRNS;
  p: pngImage.PByteArray;
  x, y: Integer;
begin
  Result := nil;
  png := TPngImage.Create; // TPNGObject
  try
    bm := TBitmap.Create;
    try
      bm.Assign (sourceBitmap);        // convert data into bitmap
      // force paletted on TBitmap, transparent for the web must be 8bit
      if paletted then
        bm.PixelFormat := pf8bit;
      png.interlaceMethod := interlaceMethod;
      png.compressionLevel := compressionLevel;
      png.Assign(bm);                  // convert bitmap into PNG
                                       // this is where the access violation occurs
    finally
      FreeAndNil(bm);
    end;
    if transparent then begin
      if png.Header.ColorType in [COLOR_PALETTE] then begin
        if (png.Chunks.ItemFromClass(TChunktRNS) = nil) then png.CreateAlpha;
        TRNS := png.Chunks.ItemFromClass(TChunktRNS) as TChunktRNS;
        if Assigned(TRNS) then TRNS.TransparentColor := bgColor;
      end;
      if png.Header.ColorType in [COLOR_RGB, COLOR_GRAYSCALE] then png.CreateAlpha;
      if png.Header.ColorType in [COLOR_RGBALPHA, COLOR_GRAYSCALEALPHA] then
      begin
        for y := 0 to png.Header.Height - 1 do begin
          p := png.AlphaScanline[y];
          for x := 0 to png.Header.Width - 1
          do p[x] := AlphaComponent(sourceBitmap.Pixel[x,y]);  // TARGB(bm.Pixel[x,y]).a;
        end;
      end;
    end;
    Result := png;
  except
    png.Free;
  end;
end;

但是使用这种方法,我在尝试保存这个特定层时得到了 EAccessViolation。对于任何其他图层(不是绘图图层),它不会使我的项目崩溃,除了这个自定义绘图。访问冲突发生在这一行:

png.分配(BM);

Bitmap32ToPNG 函数内部

您知道为什么会发生这种情况,我该如何防止这种情况发生?

编辑

我尝试改用 TBitmapLayer,因为 TPositionedLayer 可能由于某种原因缺少 Bitmap32。所以我的代码是这样的:

// adding a BitmapLayer and setting it's onPaint event to my handler
procedure TMainForm.Mynewlayer1Click(Sender: TObject);
var
  B: TBitmapLayer;
  P: TPoint;
  W, H: Single;
begin
      B := TBitmapLayer.Create(ImgView.Layers);
      with B do
      try
        Bitmap.SetSize(100,200);
        Bitmap.DrawMode := dmBlend;

        with ImgView.GetViewportRect do
          P := ImgView.ControlToBitmap(GR32.Point((Right + Left) div 2, (Top + Bottom) div 2));

        W := Bitmap.Width * 0.5;
        H := Bitmap.Height * 0.5;

        with ImgView.Bitmap do
          Location := GR32.FloatRect(P.X - W, P.Y - H, P.X + W, P.Y + H);

        Scaled := True;
        OnMouseDown := LayerMouseDown;
        OnPaint := PaintMy3Handler;
      except
        Free;
        raise;
      end;
      Selection := B;
end;

// and the PaintHandler is as follows:
procedure TMainForm.PaintMy3Handler(Sender: TObject;Buffer: TBitmap32);
var
  Cx, Cy: Single;
  W2, H2: Single;
const
  CScale = 1 / 200;
begin

  if Sender is TBitmapLayer then
    with TBitmapLayer(Sender).GetAdjustedLocation do
    begin
      // Five black pixels, five white pixels since width of the line is 5px
      Buffer.SetStipple([clBlack32, clBlack32, clBlack32, clBlack32, clBlack32,
        clWhite32, clWhite32, clWhite32, clWhite32, clWhite32]);

      W2 := (Right - Left) * 0.5;
      H2 := (Bottom - Top) * 0.5;

      Cx := Left + W2;
      Cy := Top + H2;
      W2 := W2 * CScale;
      H2 := H2 * CScale;
      Buffer.PenColor := clRed32;

      Buffer.StippleCounter := 0;
      Buffer.MoveToF(Cx-2,Top);
      Buffer.LineToFSP(Cx-2 , Bottom);

      Buffer.StippleCounter := 0;
      Buffer.MoveToF(Cx-1,Top);
      Buffer.LineToFSP(Cx-1 , Bottom);

      Buffer.StippleCounter := 0;
      Buffer.MoveToF(Cx,Top);
      Buffer.LineToFSP(Cx , Bottom);

      Buffer.StippleCounter := 0;
      Buffer.MoveToF(Cx+1,Top);
      Buffer.LineToFSP(Cx+1 , Bottom);

      Buffer.StippleCounter := 0;
      Buffer.MoveToF(Cx+2,Top);
      Buffer.LineToFSP(Cx+2 , Bottom);
    end;
end;

请记住,我使用默认图层演示应用程序。所以这只是添加的代码。我没有删除或更改演示代码中的任何内容。所以我创建了一个新图层(TBitmapLayer)并在 onPaint 上绘制。最后我想将该图层的内容保存为PNG。但似乎 onPaint 可能会在其他地方而不是实际图层上绘制。否则我不明白为什么保存的图像是空的。所以这次生成的PNG没有损坏,但它是空的......

4

1 回答 1

0

错误在于示例创建的TPositionedLayer图层不包含位图。您不能将此图层类型类型转换为 aTBitmapLayer并期望它创建图层的位图图像,就像您在以下代码中所做的那样:

  mylay := TBitmapLayer(ImgView.Layers.Items[i]);
  mylay.Bitmap.SaveToFile('C:\tmp\Layer'+IntToStr(i)+'.bmp');

我假设您执行类似保存到.png文件的操作,尽管您没有显示该代码。

示例(带TPositionedLayer图层)ImgView.Buffer用于在屏幕上绘图。您可以将其保存到 .png 文件中,如下所示:

  SavePNGTransparentX(ImgView.Buffer, 'c:\tmp\imgs\buffer.png');

但我不希望它对您的单独图层图像令人满意。

你不像TBitmapLayers以前那样使用的原因是什么?


在用户1137313评论后编辑

受到您发现自己的解决方案的启发(参考您的评论),我建议以下内容,仅在需要保存时将图层绘制到额外的位图。

从菜单项开始

procedure TMainForm.mnFileSaveClick(Sender: TObject);
begin
  SaveLayerToPng(ImgView.Layers[ImgView.Layers.Count-1], 'c:\tmp\imgs\buffer.png');
end;

如果您同时保存多个图层,您可能希望SaveLayerToPng()循环调用,并根据需要更改文件名。

然后SaveLayerToPng()程序

procedure TMainForm.SaveLayerToPng(L: TCustomLayer; FileName: string);
var
  bm32: TBitmap32;
begin
  bm32:= TBitmap32.Create;
  try
    bm32.SetSizeFrom(ImgView.Buffer);
    PaintSimpleDrawingHandler(L, bm32);
    SavePNGTransparentX(bm32, FileName);
  finally
    bm32.Free;
  end;
end;

它调用现有的PaintSimpleDrawingHandler(Sender: TObject; buffer: TBitmap32)程序进行绘制bm32,然后将其传递给 `SavePNGTransparentX() 以进行实际保存。

我使用了Graphics32示例的油漆处理程序,但您PaintMy3Handler()也可以使用。

最终结果与您的解决方案相同,只是TBitmap32仅在要保存文件时才绘制额外内容。

于 2015-04-18T08:20:39.580 回答