德尔福>我可以在我的设备(不是 dfm)中存储位图数据吗?

发布于 2024-09-30 12:20:28 字数 1111 浏览 2 评论 0原文

我在一个单元中声明了一个类,它需要使用特定的位图。 它在我的测试单元的 DFM 中声明如下:

  object ImgTop: TImage
    Left = 208
    Top = 568
    Width = 777
    Height = 41
    Picture.Data = {
      0A544A504547496D616765A1CF0000FFD8FFE000104A46494600010101025802
      [truncated]
      };

但在我的最终单元中,我不会有 dfm。 那么有没有办法可以在我的单位申报呢?

===

谢谢,我现在似乎可以使用它了,对位图的大小限制感到遗憾:( 这就是我所做的:

在我的文件夹中有这些文件:

imgleft.bmp
imgtop.bmp

这是我的资源文件,名为 ScanOCRres.rc:

1 RT_BITMAP "imgtop.bmp"
2 RT_BITMAP "imgleft.bmp"

我已将其设置为使用 C:\Program Files\Borland\Delphi 7\bin\brcc32.exe 自动执行

它生成了该文件

ScanOCRres.RES

在我单位我有 我

{$R *.dfm}
{$R ScanOCRres.RES}

这是我的代码:

var
  abmp : TBitmap;
begin
  abmp := TBitmap.create;
  abmp.LoadFromResourceID(SysInit.HInstance, 1);
  abmp.free;
end;

在 LoadFromResourceID 行收到此错误消息:

Project Project1.exe 引发异常类 EAccessViolation,并显示消息“模块 'Project1.exe' 中地址 0040A2C8 处的访问冲突”。 读取地址 00000001'

I have a class declared in a unit, and it needs to work with a specific bitmap.
It is declared in the DFM of my test unit like this:

  object ImgTop: TImage
    Left = 208
    Top = 568
    Width = 777
    Height = 41
    Picture.Data = {
      0A544A504547496D616765A1CF0000FFD8FFE000104A46494600010101025802
      [truncated]
      };

But in my final unit I won't be having a dfm.
So is there a way I can declare it in my unit?

===

Thanks, I seem to have it working now, pitty about the size limitations on bitmaps :(
Here's what I did:

In my folder are these files:

imgleft.bmp
imgtop.bmp

This is my resource file called ScanOCRres.rc:

1 RT_BITMAP "imgtop.bmp"
2 RT_BITMAP "imgleft.bmp"

I have set it to automatically execute with C:\Program Files\Borland\Delphi 7\bin\brcc32.exe

It generated the file

ScanOCRres.RES

In my unit I have
implementation

{$R *.dfm}
{$R ScanOCRres.RES}

And here's my code:

var
  abmp : TBitmap;
begin
  abmp := TBitmap.create;
  abmp.LoadFromResourceID(SysInit.HInstance, 1);
  abmp.free;
end;

I get this error message on the LoadFromResourceID line:

Project Project1.exe raised exception class EAccessViolation with message 'Access violation at address 0040A2C8 in module 'Project1.exe'. Read of address 00000001'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆萌少年 2024-10-07 12:20:28

可以将其放入代码中,但使用起来不太方便。声明一个字节数组并定义图像的每个字节。祝编辑好运。要加载它,我将字节数组包装到 TMemoryStream 中,然后使用 LoadFromStream

更好的方法是将图像存储在资源中。编写一个资源脚本文件,如下所示:

1 RT_BITMAP "foo.bmp"

.rc 文件添加到您的 Delphi 项目中,它将自动链接到您的程序。在运行时,使用 TBitmap.LoadFromResourceId< 加载图像/a>:

var
  b: TBitmap;
begin
  b := TBitmap.Create;
  b.LoadFromResourceId(SysInit.HInstance, 1);

You could put it in your code, but it wouldn't be very convenient to work with. Declare an array of bytes and define each byte of your image. Good luck editing it. To load it, I'd wrap the byte array into a TMemoryStream and then use LoadFromStream.

A better way is to store the image in a resource. Write a resource script file like this:

1 RT_BITMAP "foo.bmp"

Add that .rc file to your Delphi project, and it will automatically be linked to your program. At run time, load the image with TBitmap.LoadFromResourceId:

var
  b: TBitmap;
begin
  b := TBitmap.Create;
  b.LoadFromResourceId(SysInit.HInstance, 1);
执着的年纪 2024-10-07 12:20:28

我做了这个。这很粗糙,但很有效。将组件的图片转换为 picture.data 作为字符串。

Procedure ImgToText(Com : TComponent; var str : AnsiString);
var i,c : integer;
s : tmemorystream;
b : byte;
t : array[0..3] of char;
ch : char;
begin
 s := tmemorystream.Create;
 s.WriteComponent(com);
 s.Position := 0;
 c := 0;
 repeat
  s.Read(ch,1);
  if ch = '.' then
  begin
   s.Read(t,4);
   if t = 'Data' then
    c := s.Position+5;
  end;
 until c <> 0;
 s.Position := c;
 str := '';
 i := c;
 c := 0;
 repeat
  s.ReadBuffer(b,1);
  str := str+inttohex(b,2);
  c := c+2;
  if c >= 64 then
  begin
   str := str+#13+#10;
   c := 0;
  end;
  i := i+1;
 until i = s.Size-2;
 s.Free;
end;

I made this. It's crude but it works. Converts component's picture to picture.data as a string.

Procedure ImgToText(Com : TComponent; var str : AnsiString);
var i,c : integer;
s : tmemorystream;
b : byte;
t : array[0..3] of char;
ch : char;
begin
 s := tmemorystream.Create;
 s.WriteComponent(com);
 s.Position := 0;
 c := 0;
 repeat
  s.Read(ch,1);
  if ch = '.' then
  begin
   s.Read(t,4);
   if t = 'Data' then
    c := s.Position+5;
  end;
 until c <> 0;
 s.Position := c;
 str := '';
 i := c;
 c := 0;
 repeat
  s.ReadBuffer(b,1);
  str := str+inttohex(b,2);
  c := c+2;
  if c >= 64 then
  begin
   str := str+#13+#10;
   c := 0;
  end;
  i := i+1;
 until i = s.Size-2;
 s.Free;
end;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文