PDA

View Full Version : چرخش عکس در دلفی



.:behdad:.
چهارشنبه 04 آبان 1390, 19:34 عصر
سلام

برای چرخش عکس در دلفی باید از چه کدی استفاده کنم ؟
یعنی به تمام حالات 0 و 90 و 180 و 360 درجه بچرخه

pop123
چهارشنبه 04 آبان 1390, 19:47 عصر
Procedure TurnImage(Src, Dst: TImage);
var x,y: integer;
begin
Dst.Width:= Src.Height; Dst.Height:= Src.Width;
For x:= 0 to Src.Width-1 do begin
For y:= 0 to Src.Height-1 do begin
Dst.Canvas.Pixels[(Src.Height-1)-y,x]:= Src.Canvas.Pixels[x,y];
end;
end;
end;

.:behdad:.
چهارشنبه 04 آبان 1390, 19:50 عصر
ممنونم ازتون .

من اگه بخوام یه Button بذارم که عملیات فوق رو برام انجام بده باید این کد رو توی button بنویسم یا اینکه جای دیگه ؟

.:behdad:.
چهارشنبه 04 آبان 1390, 20:16 عصر
در ضمن پروسیجر Turn Image رو از کجا اوردید ؟

یوسف زالی
چهارشنبه 04 آبان 1390, 22:53 عصر
دوست من شما کمی در مورد خود دلفی بیشتر مطلب بخون.
اون پروسیجر رو برای شما نوشتن که وقتی لازم داشتی در رویداد دکمه اون رو فراخوانی کنی.

سعید صابری
چهارشنبه 04 آبان 1390, 23:21 عصر
در ضمن پروسیجر Turn Image رو از کجا اوردید ؟

از جایی نیاوردند نوشتن.
در button اینجوری مینویسی


TurnImage(image1(که عکس درون اون قرار داره), image2)

mbshareat
پنج شنبه 05 آبان 1390, 08:57 صبح
سلام خدمت دوستان
من یه کد داشتم که نمی دونم از کجا برداشتم.
این کد می تونه تصویر رو بر اساس هر زاویه ای بچرخونه.گفتم شاید بدردتون بخوره!
اول یه فرم با یک Button و یک OpenDialog و یک Image درست کنید بعد کل کد زیر رو جایگزین کد یونیت بکنید!
(البته Button1Click رو هم با دکمه مرتبط کنین)


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Math, ExtCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
OpenDialog1: TOpenDialog;
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin

end;

// Vektor von FromP nach ToP
// Vector from FromP to ToP

function Vektor(FromP, Top: TPoint): TPoint;
begin
Result.x := Top.x - FromP.x;
Result.y := Top.y - FromP.y;
end;

// neue x Komponente des Verktors
// new x-component of the vector
function xComp(Vektor: TPoint; Angle: Extended): Integer;
begin
Result := Round(Vektor.x * cos(Angle) - (Vektor.y) * sin(Angle));
end;

// neue Y-Komponente des Vektors
// new y-component of the vector
function yComp(Vektor: TPoint; Angle: Extended): Integer;
begin
Result := Round((Vektor.x) * (sin(Angle)) + (vektor.y) * cos(Angle));
end;


function RotImage(srcbit: TBitmap; Angle: Extended; FPoint: TPoint;
Background: TColor): TBitmap;
{
srcbit: TBitmap; // Bitmap dass gedreht werden soll ; Bitmap to be rotated
Angle: extended; // Winkel in Bogenma?, angle
FPoint: TPoint; // Punkt um den gedreht wird ; Point to be rotated around
Background: TColor): TBitmap; // Hintergrundfarbe des neuen Bitmaps ;
// Backgroundcolor of the new bitmap
}
var
highest, lowest, mostleft, mostright: TPoint;
topoverh, leftoverh: integer;
x, y, newx, newy: integer;
begin
Result := TBitmap.Create;

// Drehwinkel runterrechnen auf eine Umdrehung, wenn n?tig
// Calculate angle down on one rotation, if necessary
while Angle >= (2 * pi) do
begin
angle := Angle - (2 * pi);
end;

// neue Ausma?e festlegen
// specify new size
if (angle <= (pi / 2)) then
begin
highest := Point(0,0); //OL
Lowest := Point(Srcbit.Width, Srcbit.Height); //UR
mostleft := Point(0,Srcbit.Height); //UL
mostright := Point(Srcbit.Width, 0); //OR
end
else if (angle <= pi) then
begin
highest := Point(0,Srcbit.Height);
Lowest := Point(Srcbit.Width, 0);
mostleft := Point(Srcbit.Width, Srcbit.Height);
mostright := Point(0,0);
end
else if (Angle <= (pi * 3 / 2)) then
begin
highest := Point(Srcbit.Width, Srcbit.Height);
Lowest := Point(0,0);
mostleft := Point(Srcbit.Width, 0);
mostright := Point(0,Srcbit.Height);
end
else
begin
highest := Point(Srcbit.Width, 0);
Lowest := Point(0,Srcbit.Height);
mostleft := Point(0,0);
mostright := Point(Srcbit.Width, Srcbit.Height);
end;

topoverh := yComp(Vektor(FPoint, highest), Angle);
leftoverh := xComp(Vektor(FPoint, mostleft), Angle);
Result.Height := Abs(yComp(Vektor(FPoint, lowest), Angle)) + Abs(topOverh);
Result.Width := Abs(xComp(Vektor(FPoint, mostright), Angle)) + Abs(leftoverh);

// Verschiebung des FPoint im neuen Bild gegenüber srcbit
// change of FPoint in the new picture in relation on srcbit
Topoverh := TopOverh + FPoint.y;
Leftoverh := LeftOverh + FPoint.x;

// erstmal mit Hintergrundfarbe füllen
// at first fill with background color
Result.Canvas.Brush.Color := Background;
Result.Canvas.pen.Color := background;
Result.Canvas.Fillrect(Rect(0,0,Result.Width, Result.Height));

// Start des eigentlichen Rotierens
// Start of actual rotation
for y := 0 to srcbit.Height - 1 do
begin // Zeilen ; Rows
for x := 0 to srcbit.Width - 1 do
begin // Spalten ; Columns
newX := xComp(Vektor(FPoint, Point(x, y)), Angle);
newY := yComp(Vektor(FPoint, Point(x, y)), Angle);
newX := FPoint.x + newx - leftoverh;
// Verschieben wegen der neuen Ausma?e
newy := FPoint.y + newy - topoverh;
// Move beacause of new size
Result.Canvas.Pixels[newx, newy] := srcbit.Canvas.Pixels[x, y];
// auch das Pixel daneben füllen um Leerpixel bei Drehungen zu verhindern
// also fil lthe pixel beside to prevent empty pixels
if ((angle < (pi / 2)) or
((angle > pi) and
(angle < (pi * 3 / 2)))) then
begin
Result.Canvas.Pixels[newx, newy + 1] := srcbit.Canvas.Pixels[x, y];
end
else
begin
Result.Canvas.Pixels[newx + 1,newy] := srcbit.Canvas.Pixels[x, y];
end;
end;
end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
mybitmap, newbit: TBitMap;
begin
if OpenDialog1.Execute then
begin
mybitmap := TBitmap.Create;
mybitmap.LoadFromFile(OpenDialog1.FileName);
newbit := RotImage(mybitmap, DegToRad(45),
Point(mybitmap.Width div 2, mybitmap.Height div 2), clBlack);
Image1.Canvas.Draw(0,0, newBit);
end;
end;



end.