Solved

BMP to Binary

Posted on 2004-04-22
9
384 Views
Last Modified: 2010-04-05
Hi all!

How can I convert a bitmap (bmp) file to a binary file or binary string? I need this to send graphics to my barcode printer. Thanks.
0
Comment
Question by:delphikit
9 Comments
 
LVL 22

Accepted Solution

by:
grg99 earned 125 total points
ID: 10895580
Load the bitmap into a Timage, then use the Canvas.Pixels[x,y] property to look at and collect the pixels into the format your printer needs.

0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10895670
Bitmap is a binary file....

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10895805
umm, you could load the bitmap into a stream,
then read each individual byte and convert it to hex using inttohex function

var
 BmpStream: TMemorySTream;
 Bmp: TBitmap;
 B: Byte;
 F: TextFile;
begin
 if OpenDialog1.Execute then
 begin
  BmpStream:= TMemorySTream.Create;
  Bmp:= TBitmap.Create;
  Bmp.LoadFromFile(OpenDialog1.FileName);
  Bmp.SaveToStream(BmpStream);
 
  Assign(F, ChangeFileExt(OpenDialog1.FileName,'.dat'));
  BmpStream.Seek(0, soFromBeginning);
  for i := 1 to BmpStream.Size do
  begin
    BmpStream. Read(B, 1);
    Write(F, IntToHex(B, 2));
  end;
    WriteLn(F, '');
  CloseFile(F);
  BmpStream.Free;
  Bmp.Free;
 end;
end;

   

Shane
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Assisted Solution

by:Ivanov_G
Ivanov_G earned 125 total points
ID: 10898002

   Long time ago I used to go this. We used BarCode DLL

   http://www.tformer.com/barcode/go/barcode_dll_1.htm
0
 

Expert Comment

by:tajmiester
ID: 10904105
I use this little function, it slowish but does the job:

function FileToHex(Filename : String) : WideString;
var
  DataFile : File;
  ResultStr : WideString;
  B, Progress : Byte;
  K : Array[1..100] of Byte;
  Size, P, T : LongWord;
begin
  //Assign and open file
  AssignFile(DataFile, Filename);
  ResultStr := '';
  P := 1;
  try
    frmLoading.Show;
    Reset(DataFile, 1);
    //Check if file is too big
    Size := FileSize(DataFile);
    if Size > 30000 then begin
      ShowMessage('File is too big (more than 30KB)!');
      frmLoading.Hide;
      Result := 'ERROR';
      Exit;
    end;
    Seek(DataFile, 0);
    //Read 100B at a time
    Repeat
      BlockRead(DataFile, K, 100);
      For T := 1 to 100 do ResultStr := ResultStr + IntToHex(K[T], 2);
      P := P + 100;
      Progress := Round((P / Size) * 100);
      frmLoading.ProgressBar.Position := Progress;
    Until P >= (Size - 100);
    //Read remainer 1B at a time
    While not eof(DataFile) do
    begin
      BlockRead(DataFile, B, 1);
      ResultStr := ResultStr + IntToHex(B, 2);
    end;
    CloseFile(DataFile);
    Result := ResultStr;
  except
    ShowMessage('Could not open file!');
  end;
  frmLoading.Hide;
end;

procedure HexToFile(Filename : String; Data : WideString);
var
  DataFile : File;
  B : Byte;
  N, T : Word;
  S : ShortString;
  K : Array[1..100] of Byte;
begin
  //Assign and open file
  N := 1;
  AssignFile(DataFile, Filename);
  //Show loading bar
  frmLoading.Show;
  try
    Rewrite(DataFile, 1);
    Seek(DataFile, 0);
    //Write 100B at a time
    Repeat
      For T := 1 to 100 do
      begin
        S := Copy(Data, N, 2);
        B := Round(Conv.HexToInt(S));
        K[T] := B;
        N := N + 2;
      end;
      BlockWrite(DataFile, K, 100);
      frmLoading.ProgressBar.Position := Round(100 * (N / Length(Data)));
    Until N >= (Length(Data) - 200);
    //Write 1B at a time
    Repeat
      S := Copy(Data, N, 2);
      B := Round(Conv.HexToInt(S));
      BlockWrite(DataFile, B, 1);
      N := N + 2;
    Until N >= Length(Data);
    CloseFile(DataFile);
  except
    ShowMessage('Could not create temp file!');
  end;
  frmLoading.Hide;
end;

Tristan
0
 

Expert Comment

by:tajmiester
ID: 13283673
I agree.

Tristan
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need Delphi function to get Youtube video title 5 226
proper way to parse url in delphi 2 163
How to fill array with TArray.Create? 14 79
Base1 Encode/Decode 3 68
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now