Solved

BMP to Binary

Posted on 2004-04-22
9
416 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 
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

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

Suggested Solutions

Title # Comments Views Activity
Delphi - replicating a form 8 108
Dev express lookupcombo 3 62
Twebbrowser add css to the header 3 49
shape, triangle, dbctrlgrid 3 42
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

738 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