ASP.NET Tutorial/Development/Graphics

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Mixed drawing

<%@ Page language="c#" Inherits="MixedDrawing" CodeFile="Default.aspx.cs" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD>
    <title>MixedDrawing</title>
    </HEAD>
  <body>
    <form id="Form1" method="post" runat="server">
    </form>
  </body>
</HTML>
File: Default.aspx.cs
using System;
using System.Collections;
using System.ruponentModel;
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.Drawing.Drawing2D;
using System.IO;
public partial class MixedDrawing : System.Web.UI.Page
{
  protected void Page_Load(object sender, System.EventArgs e)
  {
    Bitmap image = new Bitmap(450, 100);
    Graphics g = Graphics.FromImage(image);
    g.SmoothingMode = SmoothingMode.AntiAlias;
    g.FillRectangle(Brushes.White, 0, 0, 450, 100);
    g.FillEllipse(Brushes.PaleGoldenrod, 120, 13, 300, 50);
    Font font = new Font("Harrington", 20, FontStyle.Bold);
    g.DrawString("Oranges are tasty!", font, Brushes.DarkOrange, 150, 20);
    System.Drawing.Image orangeImage = System.Drawing.Image.FromFile(Server.MapPath("oranges.gif"));
    g.DrawImageUnscaled(orangeImage, 0, 0);
    image.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Gif);
    g.Dispose();
    image.Dispose();
  }
}


Simple Drawing

<%@ Page language="c#" Inherits="SimpleDrawing" CodeFile="Default.aspx.cs" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD>
    <title>SimpleDrawing</title>
    </HEAD>
  <body>
    <form id="Form1" method="post" runat="server">
    </form>
  </body>
</HTML>
File: Default.aspx.cs
using System;
using System.Collections;
using System.ruponentModel;
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 SimpleDrawing : System.Web.UI.Page
{
  protected void Page_Load(object sender, System.EventArgs e)
  {
    Bitmap image = new Bitmap(300, 50);
    Graphics g = Graphics.FromImage(image);
    g.DrawRectangle(Pens.Green, 0, 0, 299, 49);
    Font font = new Font("Impact", 20, FontStyle.Regular);
    g.DrawString("This is a test.", font, Brushes.Blue, 10, 5);
        image.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
        g.Dispose();
        image.Dispose();
  }
}


ThumbmailViewer

<%@ 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.ruponentModel;
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.ruponentModel;
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();
  }
}