Resimi Döndürmek Kod

Yazdığınız makaleleri ve üyelerimizin işine yarayacağını düşündüğünüz kodlarınızı gönderebilirsiniz. Bu foruma soru sormayın!
Cevapla
RamazanG
Üye
Mesajlar: 73
Kayıt: 16 Tem 2010 03:38

Resimi Döndürmek Kod

Mesaj gönderen RamazanG »

{ Nette buldum açıklamasını Türkçe'ye çevirip yayınlıyorum:= Bu uygulama 90 derecedeki bir bitmap'ı 45 derece sağa doğru döndürür}

Kod: Tümünü seç

Uses  Math;
// Resmi FromP noktasından ToP Noktasına döndürmek

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

// Yönlendirme için Yeni Bir x-komponenti
function TForm1.xComp(Vektor: TPoint; Angle: Extended): Integer;
begin
  Result := Round(Vektor.x * cos(Angle) - (Vektor.y) * sin(Angle));
end;

// Yönlendirme için Yeni Bir y-komponenti
function TForm1.yComp(Vektor: TPoint; Angle: Extended): Integer;
begin
  Result := Round((Vektor.x) * (sin(Angle)) + (vektor.y) * cos(Angle));
end;


function TForm1.RotImage(srcbit: TBitmap; Angle: Extended; FPoint: TPoint;
  Background: TColor): TBitmap;
{
 srcbit: TBitmap; // Döndürülecek olan Bitmap
 Angle: extended; // döndürmek için değer
 FPoint: TPoint;  // Döndürülecek olan mesafeye bir işaretçi
 Background: TColor): TBitmap; // Yeni bitmap'ın arka plan rengi
}
var 
  highest, lowest, mostleft, mostright: TPoint;
  topoverh, leftoverh: integer;
  x, y, newx, newy: integer;
begin
  Result := TBitmap.Create;

 
  // Eğer gerekli ise bir döndürme hesabı yap
  while Angle >= (2 * pi) do
  begin
    angle := Angle - (2 * pi);
  end;

  
  // Yeni alanı
  if (angle <= (pi / 2)) then
  begin
    highest := Point(0,0);                        //üst
    Lowest := Point(Srcbit.Width, Srcbit.Height); //alt
    mostleft := Point(0,Srcbit.Height);            //sol
    mostright := Point(Srcbit.Width, 0);             //sağ
  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);

  
  // Yeni resim içindeki Fpoint değerini değiştir
  Topoverh := TopOverh + FPoint.y;
  Leftoverh := LeftOverh + FPoint.x;

 
  // İlk önce arka plan rengini doldur
  Result.Canvas.Brush.Color := Background;
  Result.Canvas.pen.Color   := background;
  Result.Canvas.Fillrect(Rect(0,0,Result.Width, Result.Height));

  
  // Yönlendirmenin başlangıç değeri
  for y := 0 to srcbit.Height - 1 do
  begin                       // Satırlar
    for x := 0 to srcbit.Width - 1 do
    begin                    // Sütunlar
      newX := xComp(Vektor(FPoint, Point(x, y)), Angle);
      newY := yComp(Vektor(FPoint, Point(x, y)), Angle);
      newX := FPoint.x + newx - leftoverh;
      
      newy := FPoint.y + newy - topoverh;
      // Yeni alan değeri nedeniyle resmi hareket ettir
      Result.Canvas.Pixels[newx, newy] := srcbit.Canvas.Pixels[x, y];
     
      // Daima boş pikselleri doldur
      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;

 
Cevapla