Ajuste de Images com Delphi for .NET

 

Veja como ajustar imagens quando o uploading é de tamanho especifico.

 

procedure GenerateThumbNail(const FileName: string;

  const ImageStream: Stream; const tWidth, tHeight: Double) ;

var

  g: System.Drawing.Image;

  thumbSize: Size;

  imgOutput: Bitmap;

  imgStream: MemoryStream;

begin

//This function creates the Thumbnail image and returns the

//image created in Byte() format

  g := System.Drawing.Image.FromStream(ImageStream) ;

  thumbSize := NewThumbSize(g.Width, g.Height, tWidth, tHeight) ;

 

  imgOutput := Bitmap.Create(g, thumbSize.Width, thumbSize.Height) ;

  imgStream := MemoryStream.Create;

  imgOutput.Save(imgStream, g.RawFormat) ;

  imgOutput.Save(Path.Combine('c:\LocationOnServer',FileName)) ;

  g.Dispose;

  imgOutput.Dispose;

end;

 

function NewThumbSize(const currentwidth, currentheight,

  newWidth, newHeight: Double): Size;

var

  tempMultiplier: Double;

  NewSize: Size;

begin

  if currentheight > currentwidth then

    tempMultiplier := newHeight / currentheight

  else

    tempMultiplier := newWidth / currentwidth;

 

  NewSize := Size.Create(Convert.ToInt32(

    currentwidth * tempMultiplier), Convert.ToInt32(

    currentheight * tempMultiplier)) ;

  Result := NewSize;

end;

 

//Usage:

GenerateThumbNail('ThumbnameOnServer',

  ImageFile.PostedFile.InputStream, thumbWidth, thumbHeight) ;

 

//Where

//ImageFile: System.Web.UI.HtmlControls.HtmlInputFile;