Link to home
Start Free TrialLog in
Avatar of ka1a
ka1a

asked on

copy a part of a picture to a new file (.BMP or .JPG)

The source files (.BMP or .JPG) are the size 640 X 480 or bigger.  I need to make the picture visible on screen.  So far no problem.  What I need is a peace of the picture copied into a new file (the size has to be 200 X 140).

The questions:
1) Is it possible to place a rectangle on top of the picture (200 X 140) so it can be moved by the mouse to the correct position in to the picture.
2) If question one is answered with yes.  Is it possible to copy the part of the picture inside the rectangle to a new file (.BMP or .JPG).
3) Is it possible to reduce the colors from 24 bits to 8 bits (256 colors), otherwise the size of the file will be to big (.BMP) or is this by .JPG no problem.  The size of the new file has to be arround 30 Kb maximum.  How do I have to do that.

Is it possible to give a little example of the procedures and funtions needed to do this (to select the part of the picture, to save the part into a new file and to reduce the colors if nescesary).

Thanks in advance,
Dirk.

PS Sorry for my poor English.
Avatar of Epsylon
Epsylon

1) Sorry, but that will take hours. I have no time for that.
2+3) Take a look at this code:


const x_size = 50;
      y_size = 50;
      x_offset = 100;
      y_offset = 100;
var p: TPicture;
    j: TJPEGImage;
    b: TBitmap;
    src, dest: TRect;
begin
  src := Bounds(x_offset, y_offset, x_size, y_size);
  dest := Bounds(0, 0, x_size, y_size);
  p := TPicture.Create;
  p.LoadFromFile('c:\my documents\pictures\kerstkaart.bmp');
  b := TBitmap.Create;
  b.Width := dest.Right - dest.Left + 1;
  b.Height := dest.Bottom - dest.Top + 1;
  b.Canvas.CopyRect(dest, p.Bitmap.Canvas, src);
  j := TJPEGImage.Create;
  j.Assign(b);
  j.SaveToFile('image.jpg');
  j.Free;
  b.Free;
  p.Free;
end;


Regards,

Epsylon.
Avatar of ka1a

ASKER

Thanks Epsylon,

How can I get the prommised points to You?

Dirk.
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

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
oops eps, sorry, didn't see your comment ;-)
Never mind   :o)
Avatar of ka1a

ASKER

Meikl,

I just tried youre unit.

I can select a picture (.BMP or .JPG).  Yhe picture becomes visible in image1.

There is a rectange visible.  I can move it with the mouse.

But in image2 there becomes nothing visible.  When push the save button an empty .BMP file is saved.  There are no errormessages.

What am I doing wrong.  I copied your text so I didn't make any type errors.

Any suggestions.

Thanks,
Dirk.
hi dirk,

there is a checkbox, if the checkbox checked then the cutting is active by the mouseevent of the rectangle (PaintBox).

hope thats it ;-)

meikl
Avatar of ka1a

ASKER

Meikl,

If the checkbox is checked or not, it makes no difference.

If you must find out what other reason it could be, please let me now.

Thanks,
Dirk.

PS Is it possible that I did make a mistake by constructing the Form? (A component on the wrong parrent or somthing like that?)
hi ka1a,

here is the form as text, leave your email here, then i send you the little project

object Form1: TForm1
  Left = 200
  Top = 102
  Width = 544
  Height = 375
  Caption = 'Form1'
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Splitter1: TSplitter
    Left = 249
    Top = 73
    Width = 3
    Height = 278
    Cursor = crHSplit
  end
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 536
    Height = 73
    Align = alTop
    Caption = 'Panel1'
    TabOrder = 0
    object CheckBox1: TCheckBox
      Left = 336
      Top = 8
      Width = 97
      Height = 17
      Caption = 'CheckBox1'
      TabOrder = 0
    end
    object Button1: TButton
      Left = 24
      Top = 8
      Width = 75
      Height = 25
      Caption = 'Button1'
      TabOrder = 1
      OnClick = Button1Click
    end
    object Button2: TButton
      Left = 112
      Top = 8
      Width = 75
      Height = 25
      Caption = 'Button2'
      TabOrder = 2
      OnClick = Button2Click
    end
    object Button3: TButton
      Left = 304
      Top = 40
      Width = 75
      Height = 25
      Caption = 'Button3'
      TabOrder = 3
      OnClick = Button3Click
    end
    object Edit1: TEdit
      Left = 392
      Top = 24
      Width = 121
      Height = 21
      TabOrder = 4
      Text = 'Edit1'
    end
    object Edit2: TEdit
      Left = 392
      Top = 48
      Width = 121
      Height = 21
      TabOrder = 5
      Text = 'Edit2'
    end
  end
  object ScrollBox1: TScrollBox
    Left = 0
    Top = 73
    Width = 249
    Height = 278
    Align = alLeft
    TabOrder = 1
    object Image1: TImage
      Left = 0
      Top = 0
      Width = 105
      Height = 105
      AutoSize = True
    end
    object PaintBox1: TPaintBox
      Left = 120
      Top = 32
      Width = 105
      Height = 105
      OnMouseDown = PaintBox1MouseDown
      OnMouseMove = PaintBox1MouseMove
      OnMouseUp = PaintBox1MouseUp
      OnPaint = PaintBox1Paint
    end
  end
  object ScrollBox2: TScrollBox
    Left = 252
    Top = 73
    Width = 284
    Height = 278
    Align = alClient
    TabOrder = 2
    object Image2: TImage
      Left = 0
      Top = 0
      Width = 280
      Height = 274
      AutoSize = True
      Center = True
    end
  end
  object OpenPictureDialog1: TOpenPictureDialog
    Filter = 'Bitmaps (*.bmp)|*.bmp|JPG (*.jpg)|*.jpg'
    Left = 216
    Top = 16
  end
  object SavePictureDialog1: TSavePictureDialog
    Filter = 'Bitmaps (*.bmp)|*.bmp|JPeg (*.jpg)|*.jpg'
    Left = 264
    Top = 24
  end
end

meikl
Avatar of ka1a

ASKER

Meikl,

Please send the project to:

    dirk.peeters@glo.be

Thanks,
Dirk.

PS: You realy save my day with this one, thanks again.
is sent just now ;-)