Criando thumbnails de imagens

<< Click to Display Table of Contents >>

Navigation:  ASP.NET > ASP Controls >

Criando thumbnails de imagens

Previous pageReturn to chapter overviewNext page

Default.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

  <title></title>

</head>

<body>

  <form id="form1" runat="server">

    <asp:FileUpload ID="FileUpload1" runat="server" BorderStyle="None" Width="360px" />  

    &nbsp;  

    <br />

    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Enviar</asp:LinkButton>

    <br />

    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>   </form>

</body>

</html>

 

Default.aspx.cs

 

using System;

using System.Drawing;

 

public partial class _Default : System.Web.UI.Page

{

  protected void Page_Load(object sender, EventArgs e)

   {

   }

 

  protected void LinkButton1_Click(object sender, EventArgs e)

   {

      string arquivo = Server.MapPath(FileUpload1.FileName);

       FileUpload1.SaveAs(arquivo);

 

      //Abre foto

      Image abre_imagem = Image.FromFile(arquivo);

 

      // calcula largura em proporção (50%)

      int largura = abre_imagem.Width * 50 / abre_imagem.Height;

 

      // 50 = pixels

      Image.GetThumbnailImageAbort dummyCallBack = new Image.GetThumbnailImageAbort(ThumbnailCallback);

      Image thumbNailImg = abre_imagem.GetThumbnailImage(largura, 50, dummyCallBack, IntPtr.Zero);

 

      // dados dentro dos parenteses: largura, altura...

       thumbNailImg.Save(Server.MapPath("thumbnail.jpg")); //salva thumbnail

       Label1.Text = Server.MapPath("thumbnail.jpg");

 

      //fechando conexoes

       thumbNailImg.Dispose();

       abre_imagem.Dispose();

   }

 

  public bool ThumbnailCallback()

   {

      return false;

   }

}