Solved

Create an image from a string

Posted on 2008-06-23
7
286 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
ID: 21852835
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
ID: 21852840
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
ID: 21853259
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 13

Expert Comment

by:ThievingSix
ID: 21853531
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
ID: 21853533
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
ID: 21854387
0
 

Author Comment

by:lsid
ID: 21882156
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Correct Component for Shopping Cart. 2 105
Convert a string into a TDateTime 5 60
can't find the executable in Simulator 1 90
How to convert memory stream to PDF file 6 52
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
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.

786 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