Skip to content

Instantly share code, notes, and snippets.

@mayuki
Created December 18, 2010 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayuki/696fe81268b2daa7f9e4 to your computer and use it in GitHub Desktop.
Save mayuki/696fe81268b2daa7f9e4 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace WebApplication3
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Canvas canvas = new Canvas
{
Width = 200,
Height = 200,
Background = Brushes.LightGray
};
canvas.Children.Add(new Label { Content = "初春かわいい" });
canvas.Arrange(new Rect(0, 0, canvas.Width, canvas.Height));
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((Int32)canvas.Width, (Int32)canvas.Height, 96, 96, PixelFormats.Default);
renderTargetBitmap.Render(canvas);
MemoryStream memoryStream = new MemoryStream();
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
encoder.Save(memoryStream);
Response.ContentType = "image/png";
Response.ClearContent();
Response.BinaryWrite(memoryStream.ToArray());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment