Solved

Create an image from a string

Posted on 2008-06-23
7
284 Views
Last Modified: 2013-11-23
How can I  create an image (jpg) containing a given string using Delphi 7? For example if the given string is '15jg' , to be converted to an rectangle bitmap and then this bitmap will be converted to a jpg and saved to a file  
0
Comment
Question by:lsid
  • 4
  • 2
7 Comments
 
LVL 13

Expert Comment

by:ThievingSix
Comment Utility
Code for the form(right click new form and click "view as text" then paste this):
object Form1: TForm1

  Left = 274

  Top = 194

  Width = 244

  Height = 194

  Caption = 'Text2Jpg'

  Color = clBtnFace

  Font.Charset = DEFAULT_CHARSET

  Font.Color = clWindowText

  Font.Height = -11

  Font.Name = 'MS Sans Serif'

  Font.Style = []

  OldCreateOrder = False

  PixelsPerInch = 96

  TextHeight = 13

  object Label1: TLabel

    Left = 5

    Top = 10

    Width = 24

    Height = 13

    Caption = 'Text:'

  end

  object Edit1: TEdit

    Left = 35

    Top = 5

    Width = 196

    Height = 21

    TabOrder = 0

  end

  object Button1: TButton

    Left = 5

    Top = 110

    Width = 226

    Height = 46

    Caption = 'Save'

    TabOrder = 1

    OnClick = Button1Click

  end

  object GroupBox1: TGroupBox

    Left = 5

    Top = 30

    Width = 111

    Height = 76

    Caption = 'Picture Size'

    TabOrder = 2

    object Label2: TLabel

      Left = 10

      Top = 25

      Width = 31

      Height = 13

      Caption = 'Width:'

    end

    object Label3: TLabel

      Left = 10

      Top = 50

      Width = 34

      Height = 13

      Caption = 'Height:'

    end

    object Edit2: TEdit

      Left = 50

      Top = 20

      Width = 46

      Height = 21

      TabOrder = 0

      Text = '300'

    end

    object Edit3: TEdit

      Left = 50

      Top = 45

      Width = 46

      Height = 21

      TabOrder = 1

      Text = '200'

    end

  end

  object GroupBox2: TGroupBox

    Left = 120

    Top = 30

    Width = 111

    Height = 76

    Caption = 'Text Position'

    TabOrder = 3

    object Label4: TLabel

      Left = 10

      Top = 25

      Width = 10

      Height = 13

      Caption = 'X:'

    end

    object Label5: TLabel

      Left = 10

      Top = 50

      Width = 10

      Height = 13

      Caption = 'Y:'

    end

    object Edit4: TEdit

      Left = 50

      Top = 20

      Width = 46

      Height = 21

      TabOrder = 0

      Text = '10'

    end

    object Edit5: TEdit

      Left = 50

      Top = 45

      Width = 46

      Height = 21

      TabOrder = 1

      Text = '10'

    end

  end

end

Open in new window

0
 
LVL 13

Expert Comment

by:ThievingSix
Comment Utility
Code for the unit:
unit Unit1;
 

interface
 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, Jpeg;
 

type

  TForm1 = class(TForm)

    Edit1: TEdit;

    Label1: TLabel;

    Button1: TButton;

    GroupBox1: TGroupBox;

    Label2: TLabel;

    Label3: TLabel;

    Edit2: TEdit;

    Edit3: TEdit;

    GroupBox2: TGroupBox;

    Label4: TLabel;

    Label5: TLabel;

    Edit4: TEdit;

    Edit5: TEdit;

    procedure Button1Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;
 

var

  Form1: TForm1;
 

implementation
 

{$R *.dfm}
 

procedure TForm1.Button1Click(Sender: TObject);

var

  Bitmap : TBitmap;

  JpegImage : TJpegImage;

  SaveDialog : TSaveDialog;

  FileName : String;

begin

  Bitmap := TBitmap.Create;

  Bitmap.Height := StrToIntDef(Edit3.Text,200);

  Bitmap.Width := StrToIntDef(Edit2.Text,300);

  Bitmap.Canvas.TextOut(StrToIntDef(Edit4.Text,10),StrToIntDef(Edit5.Text,10),Edit1.Text);

  JpegImage := TJpegImage.Create;

  JpegImage.Assign(Bitmap);

  SaveDialog := TSaveDialog.Create(nil);

  SaveDialog.Filter := 'JPEG Image Files(*.jpg, *.jpeg)|*.jpg;*.jpeg';

  SaveDialog.Execute;

  FileName := SaveDialog.FileName;

  If FileName = '' Then

    begin

    Bitmap.Free;

    JpegImage.Free;

    Exit;

  end

  Else

    begin

    If (LowerCase(ExtractFileExt(FileName)) <> 'jpg') Or (LowerCase(ExtractFileExt(FileName)) <> 'jpeg') Then

      begin

      FileName := FileName + '.jpg';

    end;

    If FileExists(FileName) Then

      begin

      If MessageDlg('Do you want to overwrite ' + FileName + '?',mtConfirmation,[mbYes,mbCancel],0) = mrYes Then

        begin

        JpegImage.SaveToFile(FileName);

        Bitmap.Free;

        JpegImage.Free;

        Exit;

      end

      Else

        begin

        Bitmap.Free;

        JpegImage.Free;

        Exit;

      end;

    end;

    JpegImage.SaveToFile(FileName);

  end;

  Bitmap.Free;

  JpegImage.Free;

end;
 

end.

Open in new window

0
 

Author Comment

by:lsid
Comment Utility
Thank you for the quick response
Yes, this is very closed with what I wanted.  If I wanted to have a background from an already image inside the new generated image? And also I will prefer that the string in the image will keep the font and size with which is typed in the edit box.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 13

Expert Comment

by:ThievingSix
Comment Utility
Form Code:
object Form1: TForm1

  Left = 274

  Top = 194

  Width = 244

  Height = 295

  Caption = 'Text2Jpg'

  Color = clBtnFace

  Font.Charset = DEFAULT_CHARSET

  Font.Color = clWindowText

  Font.Height = -11

  Font.Name = 'MS Sans Serif'

  Font.Style = []

  OldCreateOrder = False

  PixelsPerInch = 96

  TextHeight = 13

  object Label1: TLabel

    Left = 5

    Top = 10

    Width = 24

    Height = 13

    Caption = 'Text:'

  end

  object Edit1: TEdit

    Left = 35

    Top = 5

    Width = 196

    Height = 21

    TabOrder = 0

  end

  object Button1: TButton

    Left = 5

    Top = 210

    Width = 226

    Height = 46

    Caption = 'Save'

    TabOrder = 1

    OnClick = Button1Click

  end

  object GroupBox1: TGroupBox

    Left = 5

    Top = 30

    Width = 111

    Height = 76

    Caption = 'Picture Size'

    TabOrder = 2

    object Label2: TLabel

      Left = 10

      Top = 25

      Width = 31

      Height = 13

      Caption = 'Width:'

    end

    object Label3: TLabel

      Left = 10

      Top = 50

      Width = 34

      Height = 13

      Caption = 'Height:'

    end

    object Edit2: TEdit

      Left = 50

      Top = 20

      Width = 46

      Height = 21

      TabOrder = 0

      Text = '300'

    end

    object Edit3: TEdit

      Left = 50

      Top = 45

      Width = 46

      Height = 21

      TabOrder = 1

      Text = '200'

    end

  end

  object GroupBox2: TGroupBox

    Left = 120

    Top = 30

    Width = 111

    Height = 76

    Caption = 'Text Position'

    TabOrder = 3

    object Label4: TLabel

      Left = 10

      Top = 25

      Width = 10

      Height = 13

      Caption = 'X:'

    end

    object Label5: TLabel

      Left = 10

      Top = 50

      Width = 10

      Height = 13

      Caption = 'Y:'

    end

    object Edit4: TEdit

      Left = 50

      Top = 20

      Width = 46

      Height = 21

      TabOrder = 0

      Text = '10'

    end

    object Edit5: TEdit

      Left = 50

      Top = 45

      Width = 46

      Height = 21

      TabOrder = 1

      Text = '10'

    end

  end

  object GroupBox3: TGroupBox

    Left = 5

    Top = 110

    Width = 226

    Height = 96

    Caption = 'GroupBox3'

    TabOrder = 4

    object Label6: TLabel

      Left = 10

      Top = 40

      Width = 32

      Height = 13

      Caption = 'Image:'

    end

    object CheckBox1: TCheckBox

      Left = 10

      Top = 20

      Width = 146

      Height = 17

      Caption = 'Overlay on another image'

      TabOrder = 0

      OnClick = CheckBox1Click

    end

    object Edit6: TEdit

      Left = 45

      Top = 35

      Width = 171

      Height = 21

      Enabled = False

      TabOrder = 1

    end

    object Button2: TButton

      Left = 10

      Top = 60

      Width = 206

      Height = 25

      Caption = 'Select Image'

      Enabled = False

      TabOrder = 2

      OnClick = Button2Click

    end

  end

end

Open in new window

0
 
LVL 13

Expert Comment

by:ThievingSix
Comment Utility
Unit code:
unit Unit1;
 

interface
 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, Jpeg;
 

type

  TForm1 = class(TForm)

    Edit1: TEdit;

    Label1: TLabel;

    Button1: TButton;

    GroupBox1: TGroupBox;

    Label2: TLabel;

    Label3: TLabel;

    Edit2: TEdit;

    Edit3: TEdit;

    GroupBox2: TGroupBox;

    Label4: TLabel;

    Label5: TLabel;

    Edit4: TEdit;

    Edit5: TEdit;

    GroupBox3: TGroupBox;

    CheckBox1: TCheckBox;

    Label6: TLabel;

    Edit6: TEdit;

    Button2: TButton;

    procedure Button1Click(Sender: TObject);

    procedure CheckBox1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;
 

var

  Form1: TForm1;
 

implementation
 

{$R *.dfm}
 

procedure TForm1.Button1Click(Sender: TObject);

var

  Bitmap : TBitmap;

  JpegImage : TJpegImage;

  SaveDialog : TSaveDialog;

  FileName : String;

begin

  Bitmap := TBitmap.Create;

  If CheckBox1.Checked And (Not(FileExists(Edit6.Text))) Then

    begin

    CheckBox1.Checked := False;

    CheckBox1.OnClick(Sender);

  end;

  If CheckBox1.Checked Then

    begin

    Bitmap.LoadFromFile(Edit6.Text);

  end

  Else

    begin

    Bitmap.Height := StrToIntDef(Edit3.Text,200);

    Bitmap.Width := StrToIntDef(Edit2.Text,300);

  end;

  Bitmap.Canvas.Font := Edit1.Font;

  Bitmap.Canvas.TextOut(StrToIntDef(Edit4.Text,10),StrToIntDef(Edit5.Text,10),Edit1.Text);

  JpegImage := TJpegImage.Create;

  JpegImage.Assign(Bitmap);

  SaveDialog := TSaveDialog.Create(nil);

  SaveDialog.Filter := 'JPEG Image Files(*.jpg, *.jpeg)|*.jpg;*.jpeg';

  SaveDialog.Execute;

  FileName := SaveDialog.FileName;

  SaveDialog.Free;

  If FileName = '' Then

    begin

    Bitmap.Free;

    JpegImage.Free;

    Exit;

  end

  Else

    begin

    If (LowerCase(ExtractFileExt(FileName)) <> '.jpg') And (LowerCase(ExtractFileExt(FileName)) <> '.jpeg') Then

      begin

      FileName := FileName + '.jpg';

    end;

    If FileExists(FileName) Then

      begin

      If MessageDlg('Do you want to overwrite ' + FileName + '?',mtConfirmation,[mbYes,mbCancel],0) = mrYes Then

        begin

        JpegImage.SaveToFile(FileName);

        Bitmap.Free;

        JpegImage.Free;

        Exit;

      end

      Else

        begin

        Bitmap.Free;

        JpegImage.Free;

        Exit;

      end;

    end;

    JpegImage.SaveToFile(FileName);

  end;

  Bitmap.Free;

  JpegImage.Free;

end;
 

procedure TForm1.CheckBox1Click(Sender: TObject);

begin

  Edit6.Enabled := CheckBox1.Checked;

  Button2.Enabled := CheckBox1.Checked;

end;
 

procedure TForm1.Button2Click(Sender: TObject);

var

  OpenDialog : TOpenDialog;

begin

  OpenDialog := TOpenDialog.Create(nil);

  OpenDialog.Filter := 'Bitmap Image(*.bmp)|*.bmp';

  OpenDialog.Options := [ofFileMustExist];

  OpenDialog.Execute;

  Edit6.Text := OpenDialog.FileName;

  OpenDialog.Free;

end;
 

end.

Open in new window

0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 500 total points
Comment Utility
0
 

Author Comment

by:lsid
Comment Utility
Thank you again, It's almost what I wanted with the exception of  transparency. I will prefer the text which is drawing over the image to loose his background color (say white ) . I tried to use the follow code (inside the code sent by you) bbut without success  :

procedure TForm1.Button1Click(Sender: TObject);
var
begin
...
  Bitmap.Transparent := true;
  Bitmap.TransparentMode :=  tmFixed ;
  Bitmap.TransparentColor := clwhite;
...
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

743 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

8 Experts available now in Live!

Get 1:1 Help Now