Delphi 7 Glyph.Data十六进制到数据URI

时间:2016-06-07 08:31:14

标签: c# html delphi delphi-7

我正在尝试将Delphi-7 ui转换为Web界面。 Delphi将属性Glyph.Data中的按钮图标存储为十六进制字符串。例如:

object Btn1: TSpeedButton
        Left = -1
        Top = -1
        Width = 26
        Height = 27
        Anchors = [akTop, akRight]
        Flat = True
        Glyph.Data = {
          36060000424D3606000000000000360000002800000020000000100000000100
          1800000000000006000000000000000000000000000000000000BECACDBECACD
          BECACDBECACDBECACDBECACDD9D3E6BAB4E3E8E2E8BECACD9A96DE0000CC0000
          CC5E5BD6BECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACD8D
          A2A7BECACDBECACDBECACD8C8C8C8C8C8C8DA2A7BECACDBECACDBECACDBECACD
          BECACDBECACDBECACDD9D3E6100FCD0000CC100FCDE8E2E93E3DD20000CC0000
          CC0000CCBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACD64646464
          6464646464BECACD8C8C8C646464646464646464BECACDBECACDBECACDBECACD
          BECACDBECACDBECACDBAB4E40000CC0000CC0000CCBAB4E44E4CD60000CC0000
          CC100FCDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACD64646464
          64646464648DA2A78C8C8C646464646464646464BECACDBECACDBECACDBECACD
          BECACDBAB4E4D9D3E6E8E2E91F1ECF0000CC2F2ED0BECACDC9C3E54E4CD63E3D
          D2AAA5E0BECACDBECACDBECACDBECACDBECACD8DA2A78DA2A7BECACD64646464
          6464646464BECACDBECACD8C8C8C8C8C8C8DA2A7BECACDBECACDBECACDBECACD
          6D6AD90000CC0000CCBAB4E4BECACDC9C3E5BECACDBECACDBECACDBECACDBECA
          CDBECACDBECACDBECACDBECACDBECACD8DA2A76464646464648DA2A7BECACDBE
          CACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACD
          3E3DD20000CC0000CC7B78DBBECACDBECACDBECACDBECACDBECACDBECACDBECA
          CDBECACDBECACDBECACDBECACDBECACD8C8C8C6464646464648DA2A7BECACDBE
          CACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACD
          AAA5E01F1ECF3E3DD2D9D3E6BECACDBECACDBECACDBECACDBECACDBECACDBECA
          CDBECACDBECACDBECACDBECACDBECACD8DA2A7646464646464BECACDBECACDBE
          CACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACD
          BECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECA
          CDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBE
          CACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACD
          2F2ED00000CC8B87DDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECA
          CDBECACDBECACDBECACDBECACDBECACD646464646464646464BECACDBECACDBE
          CACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACD
          0000CC0000CC3E3DD2BECACDBECACDBECACDBECACDBECACDBECACDBECACDBECA
          CDBECACDBECACDBECACDBECACDBECACD646464646464646464BECACDBECACDBE
          CACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACD
          6D6AD93E3DD2AAA5E0BECACDBECACDBECACDBECACDBECACDBECACDBECACDBECA
          CDBECACDBECACDBECACDBECACDBECACD8C8C8C6464648DA2A7BECACDBECACDBE
          CACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACD
          BECACDBECACDBECACDBAB4E4BECACDBECACDBECACDBECACDBECACDBECACDBECA
          CDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACD8C8C8CBECACDBE
          CACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACD
          BECACDBECACD4E4CD60000CC7B78DBBECACDBECACDBECACDBECACDBECACDBECA
          CDBECACDBECACDBECACDBECACDBECACDBECACDBECACD6464646464648C8C8CBE
          CACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACD
          BECACDBECACD4E4CD60000CC7B78DBE8E2E98B87DDC9C3E5BECACDBECACDBECA
          CDBECACDBECACDBECACDBECACDBECACDBECACDBECACD6464646464648C8C8CBE
          CACD6464648DA2A7BECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACD
          BECACDBECACDE8E2E9BAB4E4BECACD8B87DD0000CC3E3DD2BECACD7B78DB4E4C
          D6BECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACD8DA2A7BECACD64
          6464646464646464BECACD6464646464648C8C8CBECACDBECACDBECACDBECACD
          BECACDBECACDBECACDBECACDBECACDD9D3E63E3DD29A96DEBECACD4E4CD60000
          CCD9D3E6BECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBECACDBE
          CACD6464648C8C8CBECACD6464646464648C8C8CBECACDBECACD}
        NumGlyphs = 2

如何将此字符串转换为HTML的数据URI?我试图将它转换为一个字节数组来制作一个位图,但这也失败了。我写了一个小的C#Helper工具来进行对话,但我无法让它运行。它会在Image.FromStream(ms)上抛出ArgumentException。

    private void button1_Click(object sender, EventArgs e)
    {
        byte[] bytes = StringToByteArray(textBox1.Text);
        byte[] newBytes = new byte[bytes.Length - 4];
        for(var i = 4; i < bytes.Length; i++)
        {
            newBytes[i - 4] = bytes[i];
        }
        using (var ms = new MemoryStream(bytes))
        {
            Image image = Image.FromStream(ms);
            pictureBox_originalImage.Image = image;
        }
    }
    public static byte[] StringToByteArray(String hex)
    {
        int NumberChars = hex.Length;
        byte[] bytes = new byte[NumberChars / 2];
        for (int i = 0; i < NumberChars; i += 2)
            bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
        return bytes;
    }

提前致谢。

2 个答案:

答案 0 :(得分:3)

执行以下操作从此Delphi表单文件中提取Windows位图:

  1. 阅读十六进制字符串。
  2. 将十六进制字符串转换为二进制(字节数组)。如果使用Delphi,您可以使用--update-key id 单元中的HexToBin执行此操作。如果使用其他语言,则必须使用适当的函数将十六进制转换为二进制。
  3. 从字节数组中删除前4个字节。删除的这些字节包含剩余字节的长度。
  4. 将这些字节保存到文件中。
  5. 您保存的是Windows位图文件。您可能希望转换为网络友好格式,例如PNG,但我认为您已经拥有了这样做的工具。

    最后,您需要将该PNG文件编码为base64,从而形成数据URI。再说一次,我假设你知道该怎么做。

    在您提供的示例中,您还可以看到Classes属性表示该字形包含两个图像。您可能希望将提取的位图拆分为多个图像。

答案 1 :(得分:0)

请原谅我非常糟糕的英语。

有两种选择。

1)使用没有空格的文件文本    使用使用readln的算法/函数。    我在下面发布了这个approch

2)我已经意识到一些完美的功能

函数HexaBmp(const aSource,aDest:String):整数; //完成 function BmpHexa(const aSource,aDest:String):Integer; // 完成工作 函数Bin2Bin(const aSource,aDest:String):整数; //完成工作(二进制文本中的二进制) function Txt2Bin(const aSource,aDest:String):整数; //(二进制二进制文本)正在进行中...

此功能使用此子功能......

程序阅读; 开始   BlockRead(F1,PFBuf ^,SIZEFB,BytesIng) 端;

程序写作; 开始   BlockWrite(F2,BLWR,Z,BytesOut) 端;

但出于版权原因,请勿发布此信息(如果我是这些人的所有者,请不要“ 对不起...我无法解释更多。它更加复杂。 一个软性和强大的算法是必要的,但速度非常快。

但请不要担心,因为您可以使用下面我的小程序来使用第一种方法。

program HexaToBmp;

{$APPTYPE CONSOLE}

uses
  SysUtils, Windows;

const
  CHX: array ['0'..'F'] of Byte = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15);

var
  FtoW: TextFile;
  FtoR: TextFile;
  WFile: String;     // name of write file
  Buff: String;      // string buffer of read file
  vS: String;        // calc string
  FNY: Word;         // final Y
  M: Word;           // prior of Y
  Y: Word;           // in loop while read index

  procedure TextSeek(var aFtx: Text; POS: Cardinal);
  begin
    with TTextRec(aFtx) do
    begin
      BufPos:= 0;
      BufEnd:= 0;
      SetFilePointer(Handle, POS, nil, FILE_BEGIN)
    end
  end;

begin
  WFile:= StringReplace(ParamStr(1), ExtractFileExt(ParamStr(1)), 'WF', []) + '.bmp';
  AssignFile(FtoR, ParamStr(1));
  AssignFile(FtoW, WFile);
  vS:= '';

  try
    Reset(FtoR);
    ReWrite(FtoW);
    TextSeek(FtoR, 8);            // position after the first 8 byte

    while not EOF(FtoR) do
    begin
      Readln(FtoR, Buff);
      FNY:= Length(Buff) + 1;
      M:= 1;
      Y:= 2;

      while Y < FNY do
      begin
        if Buff[Y] = #$A then   // new line jump
        begin
          Write(FtoW, vS);      // writing current line
          vS:= '';
          Y:= Y + 1             // jump correct index
        end
        else
          vS:= vS + Chr(16 * CHX[Buff[M]] + CHX[Buff[Y]]);   // calc bin value

        M:= Y + 1;             // increment the variable values
        Y:= M + 1
      end
    end;

    Write(FtoW, vS)
  finally
    CloseFile(FtoR);
    CloseFile(FtoW)
  end

end.
`