Link to home
Start Free TrialLog in
Avatar of delphikit
delphikit

asked on

BMP to Binary

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.
ASKER CERTIFIED SOLUTION
Avatar of grg99
grg99

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shaneholmes
shaneholmes

Bitmap is a binary file....

Shane
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
I agree.

Tristan