Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 627
  • Last Modified:

Regarding TextFile with Images !!!!

Hi Folks,

See i have a text file generated with some data from a delphi application ,now here comes new requirement to input the barcode 1d and 2d images inside the text file.is there any way to convert images using the memory stream and i have tried this option but no luck ?this is the requirement to an existing application.where we usually generate a text file and send this to a report printer or the user himself can able to see via the report viewer apps available ?i have another option of doing that in the Rave reports available but that takes too much time and the overall functionality of the applciation will get collapsed ?Please suggest me a good option to resolve ??
Thanks in advance.Sorry for my poor english.
0
renjitkumar
Asked:
renjitkumar
  • 4
  • 3
  • 3
  • +3
1 Solution
 
RezaSadighCommented:
Hi my friend,
Maybe you can use XML file instead of Text file.
0
 
jimyXCommented:
I can think of some ideas but not sure how successfully they can be implemented.
1.
You may use the same technique that Delphi handles the dfm files. For example when you have TImage on your Form Delphi saves that image in the dfm file and when you open the project it's loaded again.

2.
You can create that as an embedded resource (res file).

3.
You can handle the images that are embedded in a file (exe, dll,...etc) samte way that the ResXplor (Delphi 7 demos) does.
0
 
renjitkumarAuthor Commented:
jimyX : The options you have mentioned are good,can u suggest me those things are possible with delphi coding stuff?
Rezasadigh : i is not feasible to XML file,because we have generated a text file for all these times.
0
Industry Leaders: 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!

 
RezaSadighCommented:
Ok, it is not too complicated. I suggest that try it.
0
 
BdLmCommented:
put a memo and a image on a form and add the TPrintForm procedure
procedure TForm1.PrintForm;
var
  DC: HDC;
  isDcPalDevice: Bool;
  MemDC: HDC;
  MemBitmap: HBITMAP;
  OldMemBitmap: HBITMAP;
  hDibHeader: THandle;
  pDibHeader: Pointer;
  hBits: THandle;
  pBits: Pointer;
  ScaleX: Double;
  ScaleY: Double;
  pPal: PLOGPALETTE;
  pal: HPALETTE;
  OldPal: HPALETTE;
  i: Integer;
begin
  {Get the screen dc}
  DC := GetDC(0);
  {Create a compatible dc}
  MemDC := CreateCompatibleDC(DC);
  {create a bitmap}
  MemBitmap := CreateCompatibleBitmap(DC, Self.Width, Self.Height);
  {select the bitmap into the dc}
  OldMemBitmap := SelectObject(MemDC, MemBitmap);

  {Lets prepare to try a fixup for broken video drivers}
  isDcPalDevice := False;
  if GetDeviceCaps(DC, RASTERCAPS) and RC_PALETTE=RC_PALETTE then
  begin
    GetMem(pPal, SizeOf(TLOGPALETTE)+(255*SizeOf(TPALETTEENTRY)));
    FillChar(pPal^, SizeOf(TLOGPALETTE)+(255*SizeOf(TPALETTEENTRY)),
             #0);
    pPal^.palVersion := $300;
    pPal^.palNumEntries := GetSystemPaletteEntries(DC, 0, 256, pPal^.palPalEntry);
    if pPal^.palNumEntries<>0 then
    begin
      pal := CreatePalette(pPal^);
      OldPal := SelectPalette(MemDC, pal, False);
      isDcPalDevice := True
    end
    else
      FreeMem(pPal, SizeOf(TLOGPALETTE)+(255*SizeOf(TPALETTEENTRY)));
  end;
  {copy from the screen to the memdc/bitmap}
  BitBlt(MemDC, 0, 0, Self.Width, Self.Height, DC, Self.Left, Self.Top, SRCCOPY);

  if isDcPalDevice=True then
  begin
    SelectPalette(MemDC, OldPal, False);
    DeleteObject(pal);
  end;
  {unselect the bitmap}
  SelectObject(MemDC, OldMemBitmap);
  {delete the memory dc}
  DeleteDC(MemDC);
  {Allocate memory for a DIB structure}
  hDibHeader := GlobalAlloc(GHND, SizeOf(TBITMAPINFO)+(SizeOf(TRGBQUAD)*
                            256));
  {get a pointer to the alloced memory}
  pDibHeader := GlobalLock(hDibHeader);

  {fill in the dib structure with info on the way we want the DIB}
  FillChar(pDibHeader^, SizeOf(TBITMAPINFO)+(SizeOf(TRGBQUAD)*
           256), #0);
  PBITMAPINFOHEADER(pDibHeader)^.biSize := SizeOf(TBITMAPINFOHEADER);
  PBITMAPINFOHEADER(pDibHeader)^.biPlanes := 1;
  PBITMAPINFOHEADER(pDibHeader)^.biBitCount := 8;
  PBITMAPINFOHEADER(pDibHeader)^.biWidth := Self.Width;
  PBITMAPINFOHEADER(pDibHeader)^.biHeight := Self.Height;
  PBITMAPINFOHEADER(pDibHeader)^.biCompression := BI_RGB;

  {find out how much memory for the bits}
  GetDIBits(DC, MemBitmap, 0, Self.Height, nil, TBITMAPINFO(pDibHeader^),
            DIB_RGB_COLORS);

  {Alloc memory for the bits}
  hBits := GlobalAlloc(GHND, PBITMAPINFOHEADER(pDibHeader)^.BiSizeImage);

  {Get a pointer to the bits}
  pBits := GlobalLock(hBits);

  {Call fn again, but this time give us the bits!}
  GetDIBits(DC, MemBitmap, 0, Self.Height, pBits, PBitmapInfo(pDibHeader)^,
            DIB_RGB_COLORS);

  {Lets try a fixup for broken video drivers}
  if isDcPalDevice=True then
  begin
    for i := 0 to (pPal^.palNumEntries-1) do
    begin
      PBitmapInfo(pDibHeader)^.bmiColors[i].rgbRed := pPal^.palPalEntry[i].peRed;
      PBitmapInfo(pDibHeader)^.bmiColors[i].rgbGreen := pPal^.palPalEntry[i].peGreen;
      PBitmapInfo(pDibHeader)^.bmiColors[i].rgbBlue := pPal^.palPalEntry[i].peBlue;
    end;
    FreeMem(pPal, SizeOf(TLOGPALETTE)+(255*SizeOf(TPALETTEENTRY)));
  end;
  {Release the screen dc}
  ReleaseDC(0, DC);
  {Delete the bitmap}
  DeleteObject(MemBitmap);

  {Start print job}
  Printer.BeginDoc;

  {Scale print size }
  ScaleX := Self.Width*3;
  ScaleY := Self.Height*3;

  {
  if Printer.PageWidth < Printer.PageHeight then
  begin
    ScaleX := Printer.PageWidth;
    ScaleY := Self.Height*(Printer.PageWidth/Self.Width);
  end
  else
  begin
    ScaleX := Self.Width*(Printer.PageHeight/Self.Height);
    ScaleY := Printer.PageHeight;
  end;
  }

  {Just incase the printer drver is a palette device}
  isDcPalDevice := False;
  if GetDeviceCaps(Printer.Canvas.Handle, RASTERCAPS) and RC_PALETTE=RC_PALETTE
                   then
  begin
    {Create palette from dib}
    GetMem(pPal, SizeOf(TLOGPALETTE)+(255*SizeOf(TPALETTEENTRY)));
    FillChar(pPal^, SizeOf(TLOGPALETTE)+(255*SizeOf(TPALETTEENTRY)),
             #0);
    pPal^.palVersion := $300;
    pPal^.palNumEntries := 256;
    for i := 0 to (pPal^.palNumEntries-1) do
    begin
      pPal^.palPalEntry[i].peRed := PBitmapInfo(pDibHeader)^.bmiColors[i].rgbRed;
      pPal^.palPalEntry[i].peGreen := PBitmapInfo(pDibHeader)^.bmiColors[i].rgbGreen;
      pPal^.palPalEntry[i].peBlue := PBitmapInfo(pDibHeader)^.bmiColors[i].rgbBlue;
    end;
    pal := CreatePalette(pPal^);
    FreeMem(pPal, SizeOf(TLOGPALETTE)+(255*SizeOf(TPALETTEENTRY)));
    OldPal := SelectPalette(Printer.Canvas.Handle, pal, False);
    isDcPalDevice := True
  end;
  {send the bits to the printer}
  StretchDiBits(Printer.Canvas.Handle, 0, 0, Round(ScaleX), Round(ScaleY),
                0, 0, Self.Width, Self.Height, pBits, PBitmapInfo(pDibHeader)^,
                DIB_RGB_COLORS, SRCCOPY);

  {Just incase you printer drver is a palette device}
  if isDcPalDevice=True then
  begin
    SelectPalette(Printer.Canvas.Handle, OldPal, False);
    DeleteObject(pal);
  end;
  {Clean up allocated memory}
  GlobalUnlock(hBits);
  GlobalFree(hBits);
  GlobalUnlock(hDibHeader);
  GlobalFree(hDibHeader);

  {end the print job}
  Printer.EndDoc;
end;

Open in new window

0
 
Ephraim WangoyaCommented:

You need a routine to convert your image into a string then enclose the string within identifying tags in the Text file eg

[BMP]
123ed5a78fb8c.........
[/BMP]

Your application must then be able to convert the string back to an image. that is the gist of the solution

There are lots of algorithms on the net to this kind of conversion. If you cant find any then I can create one for you
0
 
BdLmCommented:
if rave is an option , here come a sample with code based rave report - print image - print memo - ....  with the help of jimx here from EE
unit Unit_LibraryReportTool;
{ *****************************************************************************
  *
  *
  *       DEMO  DUMP the Content of the Borland biolife.db with RAVE reports
  *
  *
  *       http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_26887858.html?cid=239#a35148858
  *       http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_26854889.html
  *       http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_26887858.html
  *
  *****************************************************************************}

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, StdCtrls,  DB, Grids,
  DBGrids, DBTables, ComCtrls, ExtCtrls,

  //  RAVE
  RpRave, RpDefine, RpCon, RpConDS,

  RPMEMO, RPDBUTIL,


  RpBase, RpSystem, RpConBDE, DBCtrls ;

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Exit1: TMenuItem;
    ReportTable: TTable;
    DataSource1: TDataSource;
    LoadLibararyDB1: TMenuItem;
    MainStatusBar: TStatusBar;
    Report1: TMenuItem;
    StartReport1: TMenuItem;
    Panel1: TPanel;
    Label1: TLabel;
    ListBox1: TListBox;
    RvTableConnection1: TRvTableConnection;
    LibRvSystem: TRvSystem;
    RvProject1: TRvProject;
    ableReport1: TMenuItem;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    DBGrid1: TDBGrid;
    DBImage1: TDBImage;
    DBMemo1: TDBMemo;
    ReportTableSpeciesNo: TFloatField;
    ReportTableCategory: TStringField;
    ReportTableCommon_Name: TStringField;
    ReportTableSpeciesName: TStringField;
    ReportTableLengthcm: TFloatField;
    ReportTableLength_In: TFloatField;
    ReportTableNotes: TMemoField;
    ReportTableGraphic: TGraphicField;
    procedure Exit1Click(Sender: TObject);
    procedure LoadLibararyDB1Click(Sender: TObject);
    procedure StartReport1Click(Sender: TObject);
    procedure LibRvSystemPrint(Sender: TObject);
    procedure LibRvSystemAfterPreviewPrint(Sender: TObject);
  private
    { Private-Deklarationen }
    FLibraryFilename   :  String;     ///  the name of the table for report
    ///
    ///
    procedure RavePrintDBmemo  ( PrintStart, PrintEnd : Real; YPos : Real ; AReport : TBasereport ) ;
    procedure RavePrintDBmemo2 ( PrintStart, PrintEnd : Real; YPos : Real ; AReport : TBasereport ) ;
    procedure RavePrintDBBitmap(XPOS, YPos, Xsize, YSize: Real; AReport: TBasereport);
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Exit1Click(Sender: TObject);
begin
      close;
end;

procedure TForm1.LoadLibararyDB1Click(Sender: TObject);
begin
      FLibraryFilename := 'e:\biolife.db';

      ReportTable.Active := false;

      ReportTable.TableName := FLibraryFilename;

      ReportTable.Active := true;


end;

procedure TForm1.LibRvSystemAfterPreviewPrint(Sender: TObject);
begin

end;

///
///
///
procedure TForm1.LibRvSystemPrint(Sender: TObject);
const tab1=0.2;
      tab2=1.7;
      tab3=3.2;
      tab4=4.0;
      tab5=5.0;

var    i          :  Integer;
       dbstring   :  String;
begin


     LibRvSystem.SystemPrinter.Orientation := poLandScape; // or poPortrait





      with Sender as TBaseReport do
        begin






        SetFont('ARIAL',15);

        Print ('Welcome to the rave demo report project (printing biolife.db) !!');




        GoToXY(1,1);
        NewLine;
        NewLine;

        ClearTabs;
        SetTab(tab1,pjLeft,tab2, 0,0,0);
        SetTab(tab2,pjLeft,tab3, 0,0,0);
        SetTab(tab3,pjLeft,tab4, 0,0,0);
        SetTab(tab4,pjLeft,tab5, 0,0,0);

        Bold := true;
        SetFont('ARIAL',10);


        PrintTab('Species No');
        PrintTab('Category');
        PrintTab('Common_Name');
        PrintTab('species Name ');

        FinishTabBox(1);

        Newline;
        Bold := false;

        for i:= 1 to ReportTable.RecordCount  do
             begin


            // canvas.brush.color := clred;
            // rectangle (0.1, 0.1, 6, 0.2);


             newline;

             dbstring := ReportTable.FieldByName('Species No').asstring;
             PrintTab(dbstring);

             dbstring := ReportTable.FieldByName('Category').asstring;
             PrintTab(dbstring);


             dbstring := ReportTable.FieldByName('Common_Name').asstring;
             PrintTab(dbstring);


             dbstring := ReportTable.FieldByName('Species Name').asstring;
             PrintTab(dbstring);

             // FinishTabBox(1);


             NewLine;


             RavePrintDBmemo(3,5, 1, (sender as Tbasereport) );

             RavePrintDBBitmap(1,1,2,2, (sender as Tbasereport) );



             RavePrintDBmemo2(3,5, 1, (sender as Tbasereport) );


             ReportTable.Next;



             ///   1 record on 1 page

             NewPage;

             end;








        end;
end;




procedure TForm1.RavePrintDBBitmap ( XPOS, YPos , Xsize, YSize : Real ; AReport : TBasereport );
var  Bitmap              : TBitmap;
     TableGraphicField   : TGraphicField;
begin


        // TableGraphicField := ;

        Bitmap := TBitmap.Create;
        try

          GraphicFieldToBitmap( ReportTableGraphic  , Bitmap);
          AReport.PrintBitmapRect(XPos,YPos,XPos + Xsize, YPos + YSize,Bitmap);
        finally
          Bitmap.Free;
        end;

end;



procedure TForm1.RavePrintDBmemo ( PrintStart, PrintEnd : Real; YPos : Real ; AReport : TBasereport ) ;
var  MemBuf   : TDBMemoBuf;
     dbstring : String;
     i        : Integer;
     isEOL    : Boolean;
begin





  MemBuf := TDBMemoBuf.Create;
  with ReportTable do
    begin

        YPos := YPos + 0.5;

        AReport.GoToXY(PrintStart,YPos);
        MemBuf.PrintStart := PrintStart;
        MemBuf.PrintEnd := PrintEnd ;
        MemBuf.text := Reporttable.FieldByName('Notes').AsString;
        MemBuf.NoCRLF := False;
            for I := 1 to 5 do
              begin
                dbstring := AReport.GetMemoLine(MemBuf, isEOL);
                if dbstring <> '' then
                  begin
                    YPos := YPos + 0.15;
                    AReport.GoToXY(PrintEnd,YPos);
                    AReport.PrintLn(dbstring);  // PrintTab
                  end;
              end;
            YPos := YPos + 0.2;


    MemBuf.Free;
    end;
end;





procedure TForm1.RavePrintDBmemo2 ( PrintStart, PrintEnd : Real; YPos : Real ; AReport : TBasereport ) ;

var  Memobuf : TDBMemobuf;
     Lines : Integer;
begin






  With AReport do begin

    try
     Memobuf := TDBMemobuf.Create;

     Memobuf.PrintStart := TabStart(3);

     Memobuf.PrintEnd := TabEnd(3);

     MemoBuf.text := Reporttable.FieldByName('Notes').AsString;; // Table1Notes is the name of the field "Notes" in the TTable

     Lines := MemoLines(Memobuf);

     If Lines < 8 then
         begin
          Lines := 8;
         end;

     PrintMemo(Memobuf,Lines,true);


     finally
      Memobuf.Free;
     end;
  end;
end;




procedure TForm1.StartReport1Click(Sender: TObject);
begin
      ////

      MainStatusBar.SimpleText := 'Start Simple Database Documentation';


      LibRvSystem.Execute;


      MainStatusBar.SimpleText := 'Documentation -> done ! ';
end;

end.

Open in new window

0
 
Ephraim WangoyaCommented:

@renjitkumar
20 points for such a complex question, really

Here is a simple example saving a bitmap to a text file then reloading it
Unit1.pas
Unit1.dfm
0
 
renjitkumarAuthor Commented:
@ewangoya
I tried the sample code given by you.  The sample code that you have given converts the bitmap in to stream and then saves it in a text file.If you look at the text file there are junk characters. You are reloading it back to an TImage. My concern is will i be able to see the image in the text file instead of junk characters?
0
 
developmentguruCommented:
A text file is just that... a text file.  If you need to be able to see the image in a document that you can print / edit in a word processor then what you are looking for is NOT a text file.  It could be just a language issue (guessing that English is not your first language).  In this case you will need to specify if you are able to use .DOC (Microsoft word) or .ODT (Open Office Text Document), or some other file type.  Without first clarifying your need to step up to a word processing document from a plain text document AND specifying which type to use, the experts here will no longer be able to help.
0
 
RezaSadighCommented:
Yes, text file is just TEXT file. In addition to doc you can use HTML. You should know your request isn't too easy.
Good luck
0
 
renjitkumarAuthor Commented:
developmentguru: : ton thanx for your reply,what i need exactly is that currently we have a delphi application that will create a text file with some data from the database,we manually format all those design to show in the report writer (user can view that text doc by using the report viewer) and now we have a requirement to get a barcode image to print on the report screen and the barcode images we ll get from web service call (i am not sure how we are going to get that image part guess we need to invoke the web service) for now we leave this barcode image.in general technical feasibility in what way can we sort out this image on a text file or else using the rave reports can we import an image and how?

Guess this clarifies my request?if not revert back to me with queries.
Thanks in advance.
0
 
RezaSadighCommented:
Use Rave, Quick Report, Crystal Report or other repoting tools. I my self use Quick Report and some Barcode fonts for create a page that contain Barcodes.
Best regards
0
 
developmentguruCommented:
Rave is my personal choice.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now