?
Solved

BMP to Binary

Posted on 2004-04-22
9
Medium Priority
?
485 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
6 Comments
 
LVL 22

Accepted Solution

by:
grg99 earned 500 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Assisted Solution

by:Ivanov_G
Ivanov_G earned 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

864 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