ThumbmailViewer : Graphics « Development « ASP.NET Tutorial






<%@ Page language="c#" Inherits="ThumbnailsInDirectory" CodeFile="Default.aspx.cs" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD>
    <title>ThumbnailsInDirectory</title>
  </HEAD>
  <body>
    <form id="Form1" method="post" runat="server">
      
        <asp:Label id="Label1" runat="server">Directory: </asp:Label>&nbsp;
        <asp:TextBox id="txtDir" runat="server" Width="343px">c:\Windows\</asp:TextBox>
        <asp:Button id="cmdShow" runat="server" Text="Show Thumbnails" Width="123px" onclick="cmdShow_Click"></asp:Button>
                <asp:GridView ID="gridThumbs" runat="server" AutoGenerateColumns="False" Font-Names="Verdana"
                Font-Size="X-Small" GridLines="None">
                <Columns>
                    <asp:TemplateField>
                    <ItemTemplate>
          <img src='<%# GetImageUrl(Eval("FullName")) %>' />
          <%# Eval("Name") %>
          <hr>
        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
    </form>
  </body>
</HTML>

File: Default.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;


public partial class ThumbnailsInDirectory : System.Web.UI.Page
{

  protected void cmdShow_Click(object sender, System.EventArgs e)
  {
    DirectoryInfo dir = new DirectoryInfo(txtDir.Text);
    gridThumbs.DataSource = dir.GetFiles("*.bmp");

    gridThumbs.DataBind();
  }

  protected string GetImageUrl(object path)
  {
    return "Default.aspx?x=50&y=50&FilePath=" +
      Server.UrlEncode((string)path);
  }

}

File: NextPage.aspx

<%@ Page language="c#" Inherits="ThumbnailViewer" CodeFile="NextPage.aspx.cs" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD>
    <title>ThumbnailViewer</title>
  </HEAD>
  <body>
    <form id="Form1" method="post" runat="server">
    </form>
  </body>
</HTML>

File: NextPage.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


public partial class ThumbnailViewer : System.Web.UI.Page
{
  protected void Page_Load(object sender, System.EventArgs e)
  {
    if ((Request.QueryString["X"] == null) ||(Request.QueryString["Y"] == null) ||(Request.QueryString["FilePath"] == null)){
            return;
    }
       int x = Int32.Parse(Request.QueryString["X"]);
    int y = Int32.Parse(Request.QueryString["Y"]);
    string file = Server.UrlDecode(Request.QueryString["FilePath"]);

    Bitmap image = new Bitmap(x, y);
    Graphics g = Graphics.FromImage(image);

    System.Drawing.Image thumbnail = System.Drawing.Image.FromFile(file);

    g.DrawImage(thumbnail, 0, 0, x, y);

    image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    g.Dispose();
    image.Dispose();
  }
}








9.22.Graphics
9.22.1.Simple Drawing
9.22.2.Mixed drawing
9.22.3.ThumbmailViewer