Re-learning gdi+[90]: Tgpimage (10)

Source: Internet
Author: User
Tags exit count integer rowcount

This example effect chart:

Code files:Unit Unit1;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls, Grids;
Type
TForm1 = Class (Tform)
Drawgrid1:tdrawgrid;
Opendialog1:topendialog;
Button1:tbutton;
Procedure Formcreate (Sender:tobject);
Procedure Formdestroy (Sender:tobject);
Procedure Formpaint (Sender:tobject);
Procedure Button1Click (Sender:tobject);
End
Var
Form1:tform1;
Implementation
{$R *.DFM}
Uses Gdipobj, Gdipapi, Math;
Var
Img:tgpimage;
Procedure Tform1.formcreate (Sender:tobject);
Begin
{Adjust DrawGrid1 display effect}
With DrawGrid1 do
Begin
Align: = Alright;
Width: = 180;
Fixedcols: = 0;
Fixedrows: = 0;
Defaultcolwidth: = 10;
Defaultrowheight: = 10;
ColCount: = 0;
ROWCOUNT: = 1;
Defaultdrawing: = False;
ScrollBars: = Ssnone;
End
Button1.left: = Clientwidth-drawgrid1.width-button1.width;
Button1.top: = 0;
IMG: = tgpimage.create;
End
Procedure Tform1.formdestroy (Sender:tobject);
Begin
Img. Free;
End
Procedure Tform1.formpaint (Sender:tobject);
Var
G:tgpgraphics;
Begin
G: = Tgpgraphics.create (Self.Canvas.Handle);
G.drawimage (IMG, 0, 0, img.) GetWidth, IMG. GetHeight);
G.free;
End
Procedure Tform1.button1click (Sender:tobject);
Var
Palette:pcolorpalette; {Palette pointer}
Colornum:integer; {Total colors in the palette}
Colors:pgpcolor; {color array pointer in palette}
I:integer;
Begin
Opendialog1.filter: = ' All (*.png;*.gif;*.bmp;*.tif;*.jpg) |*.png;*.gif;*.bmp;*.tif;*.jpg ';
If not Opendialog1.execute then Exit;
Img. Free;
IMG: = tgpimage.create (Opendialog1.filename);
Text: = Extractfilename (Opendialog1.filename);
{Allocate memory for palette}
Getmem (Palette, IMG. Getpalettesize);
{Get Palette data}
Img. Getpalette (Palette, IMG. Getpalettesize);
Drawgrid1.colcount: = 0;
Drawgrid1.rowcount: = 1;
If palette^. Count = 0 Then
Begin
ShowMessage (' This picture has no palette ');
Exit;
End
Colornum: = palette^. Count;
Colors: = @Palette. Entries; {A pointer to an array of colors in the palette}
If Colornum < then drawgrid1.colcount: = Colornum Else
Begin
Drawgrid1.colcount: = 16;
Drawgrid1.rowcount: = colornum Div 16;
End
Drawgrid1.refresh; {Do not refresh no}
For I: = 0 to ColorNum-1 do
Begin
DrawGrid1.Canvas.Brush.Color: = Argbtocolorref (colors^);
DrawGrid1.Canvas.FillRect (Drawgrid1.cellrect (i mod, I div 16));
INC (Colors);
End
Freemem (Palette);
End
End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.