[ASP.NET] プロ生ちゃん壁紙ジェネレーター 2
先日の プロ生ちゃん 壁紙ジェネレーター 2 のコードを公開しておきます。いわゆる classic ASP.NET (Web forms) です。
サーバー側で画像を合成してます。HTML5 + JavaScript で合成は、プロ生ちゃん アイコンジェネレーター などを見てください。
壁紙ジェネレーター ASP.NET Tips
コードのポイントをまとめておきます。
GET パラメータークエリーの取得と URL か判定
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dim q = Context.Request.QueryString.Get("q") | |
If Not Uri.IsWellFormedUriString(q, UriKind.Absolute) Then | |
Exit Sub | |
End If | |
Dim url = New Uri(q) |
URL から Bitmap の生成
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dim webBmp As Bitmap | |
Using wc = New WebClient | |
webBmp = New Bitmap(wc.OpenRead(url)) | |
End Using |
サーバーのディレクトリにある画像の読み込み
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dim wallBmp = New Bitmap(System.IO.Path.Combine(Server.MapPath("./"), "wallpaper.png")) |
描画先の範囲に合せて、画像の合成
左右・上下に空きがないよう、Web 画像を拡大・縮小して必要な範囲を描画します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dim dst = New Rectangle(754, 18, 1110, 807) ' 描画先の位置・大きさ | |
Dim src = New Rectangle(0, 0, webBmp.Width, webBmp.Height) ' 参照画像の位置・大きさ | |
If dst.Width / dst.Height < webBmp.Width / webBmp.Height Then | |
Dim w = dst.Width / (dst.Height / webBmp.Height) | |
src.X = Convert.ToInt32((webBmp.Width – w) / 2) | |
src.Width = Convert.ToInt32(w) | |
Else | |
Dim h = dst.Height / (dst.Width / webBmp.Width) | |
src.Y = Convert.ToInt32((webBmp.Height – h) / 2) | |
src.Height = Convert.ToInt32(h) | |
End If | |
Dim wallBmp = New Bitmap(System.IO.Path.Combine(Server.MapPath("./"), "wallpaper.png")) | |
Dim outBmp = New Bitmap(wallBmp.Width, wallBmp.Height) | |
Using g = Graphics.FromImage(outBmp) | |
g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality | |
g.DrawImage(webBmp, dst, src, GraphicsUnit.Pixel) | |
g.DrawImage(wallBmp, 0, 0, wallBmp.Width, wallBmp.Height) | |
End Using |
Content-Type image/png としてレスポンスを返す(画像の出力)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Response.ContentType = "image/png" | |
Response.Flush() | |
outBmp.Save(Response.OutputStream, Imaging.ImageFormat.Png) | |
Response.End() |
Web に配置したとき、パーサーエラーと表示される
「パーサー エラー メッセージ: 型 '○○○’ を読み込めませんでした。」と表示される場合、aspx ファイルの CodeBehind を CodeFile に変更します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Page Language="vb" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="GeneratorWebForm" %> |