Solved

BMP to Binary

Posted on 2004-04-22
9
382 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

12 Experts available now in Live!

Get 1:1 Help Now