rendered paste body<%@ Page Language="C#"%>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing.Text" %>
<%@ Import Namespace="System.Runtime.InteropServices" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
<script runat=server >
enum RgbPart { RgbPartRed, RgbPathGreen, RgbPartBlue };
void Page_Load(object sender, EventArgs e)
{
string strWatermark = GetArg("text", "Hello World");
string imageURL = GetArg("imagepath", "");
float intLeft = float.Parse(GetArg("x", "0"));
float intTop = float.Parse(GetArg("y", "0"));
float sRotation = float.Parse(GetArg("rotate", "90"));
string fontFamily = GetArg("family", "Verdana");
float fontSize = float.Parse(GetArg("size", "12")); // assumed to be pixels
bool fontBold = bool.Parse(GetArg("bold", "false"));
bool fontItalic = bool.Parse(GetArg("italic", "false"));
bool fontUnderline = bool.Parse(GetArg("underline", "false"));
Color fontColor = GetColor(GetArg("color", "Black"));
// arbitrary limits to avoid killing servers; feel free to change in your own implementation
if (strWatermark.Length > 100)
strWatermark = strWatermark.Substring(0, 100);
if (fontSize > 400)
fontSize = 400;
// need a bitmap and graphics object in order to measure the text to determine the height and width
// of the actual bitmap and graphics objects.
SizeF size = new SizeF(1, 1);
Font font = null;
using (Bitmap bitmap = new Bitmap(1, 1))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.TextRenderingHint = TextRenderingHint.AntiAlias;
FontStyle fontStyle = FontStyle.Regular;
if (fontBold) fontStyle |= FontStyle.Bold;
if (fontItalic) fontStyle |= FontStyle.Italic;
if (fontUnderline) fontStyle |= FontStyle.Underline;
font = new Font(fontFamily, fontSize, fontStyle, GraphicsUnit.Pixel);
// could get fancy here with StringFormat options
size = g.MeasureString(strWatermark, font);
}
}
using (Bitmap bitmap = new Bitmap((int)size.Width, (int)size.Height, PixelFormat.Format32bppArgb))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
using (SolidBrush br = new SolidBrush(Color.White))
{
g.FillRectangle(br, 0, 0, bitmap.Width, bitmap.Height);
}
using (SolidBrush br = new SolidBrush(fontColor))
{
g.DrawString(strWatermark, font, br, 0, 0);
}
MemoryStream m = new MemoryStream();
Bitmap newbitmap = RotateImage(bitmap, sRotation);
newbitmap.Save(m, ImageFormat.Gif);
Bitmap objBitMap = new Bitmap(Server.MapPath(imageURL));
Graphics g1 = Graphics.FromImage(objBitMap);
g1.SmoothingMode = SmoothingMode.AntiAlias;
g1.FillRectangle(new SolidBrush(Color.Transparent ), 0, 0, objBitMap.Width, objBitMap.Height);
//Bitmap nbm = MakeTransparent(m);
g1.DrawImage(new Bitmap(MakeTransparent(m)) , intLeft, intTop);
MemoryStream m1 = new MemoryStream();
objBitMap.Save(m1, ImageFormat.Jpeg );
//newimage.Save(oMem, ImageFormat.Jpeg);
Context.Response.ContentType = "image/gif";
m1.WriteTo(Context.Response.OutputStream);
//return MakeTransparent(m);
font.Dispose();
}
}
}
string GetArg(string argName, string argDefault)
{
string arg = Request.QueryString[argName];
if ((null == arg) || (arg.Length == 0)) return argDefault;
return arg;
}
Color GetColor(string color)
{
if (color.StartsWith("#"))
{
return Color.FromArgb(IntFromHexRgbPart(color, RgbPart.RgbPartRed),
IntFromHexRgbPart(color, RgbPart.RgbPathGreen),
IntFromHexRgbPart(color, RgbPart.RgbPartBlue)
);
}
return Color.FromName(color);
}
int IntFromHexRgbPart(string hexRgb, RgbPart part)
{
if ((null == hexRgb) || (hexRgb.Length == 0) || (!(hexRgb.StartsWith("#"))))
return 0;
try
{
switch (part)
{
case RgbPart.RgbPartRed:
if (hexRgb.Length < 3) return 0;
return IntFromHex(hexRgb.Substring(1, 2));
case RgbPart.RgbPathGreen:
if (hexRgb.Length < 5) return 0;
return IntFromHex(hexRgb.Substring(3, 2));
case RgbPart.RgbPartBlue:
if (hexRgb.Length < 7) return 0;
return IntFromHex(hexRgb.Substring(5, 2));
default:
return 0;
}
}
catch { return 0; }
}
int IntFromHex(string hex)
{
return (int)byte.Parse(hex, System.Globalization.NumberStyles.HexNumber);
}
static Bitmap RotateImage(System.Drawing.Image image, float angle)
{
if (image == null)
throw new ArgumentNullException("image");
const double pi2 = Math.PI / 2.0;
// Why can't C# allow these to be const, or at least readonly
// *sigh* I'm starting to talk like Christian Graus :omg:
double oldWidth = (double)image.Width;
double oldHeight = (double)image.Height;
// Convert degrees to radians
double theta = ((double)angle) * Math.PI / 180.0;
double locked_theta = theta;
// Ensure theta is now [0, 2pi)
while (locked_theta < 0.0)
locked_theta += 2 * Math.PI;
double newWidth, newHeight;
int nWidth, nHeight; // The newWidth/newHeight expressed as ints
double adjacentTop, oppositeTop;
double adjacentBottom, oppositeBottom;
// We need to calculate the sides of the triangles based
// on how much rotation is being done to the bitmap.
// Refer to the first paragraph in the explaination above for
// reasons why.
if ((locked_theta >= 0.0 && locked_theta < pi2) ||
(locked_theta >= Math.PI && locked_theta < (Math.PI + pi2)))
{
adjacentTop = Math.Abs(Math.Cos(locked_theta)) * oldWidth;
oppositeTop = Math.Abs(Math.Sin(locked_theta)) * oldWidth;
adjacentBottom = Math.Abs(Math.Cos(locked_theta)) * oldHeight;
oppositeBottom = Math.Abs(Math.Sin(locked_theta)) * oldHeight;
}
else
{
adjacentTop = Math.Abs(Math.Sin(locked_theta)) * oldHeight;
oppositeTop = Math.Abs(Math.Cos(locked_theta)) * oldHeight;
adjacentBottom = Math.Abs(Math.Sin(locked_theta)) * oldWidth;
oppositeBottom = Math.Abs(Math.Cos(locked_theta)) * oldWidth;
}
newWidth = adjacentTop + oppositeBottom;
newHeight = adjacentBottom + oppositeTop;
nWidth = (int)Math.Ceiling(newWidth);
nHeight = (int)Math.Ceiling(newHeight);
Bitmap rotatedBmp = new Bitmap(nWidth, nHeight, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(rotatedBmp))
{
g.FillRectangle(new SolidBrush(Color.White), 0, 0, rotatedBmp.Width, rotatedBmp.Height);
// This array will be used to pass in the three points that
// make up the rotated image
Point[] points;
if (locked_theta >= 0.0 && locked_theta < pi2)
{
points = new Point[] {
new Point( (int) oppositeBottom, 0 ),
new Point( nWidth, (int) oppositeTop ),
new Point( 0, (int) adjacentBottom )
};
}
else if (locked_theta >= pi2 && locked_theta < Math.PI)
{
points = new Point[] {
new Point( nWidth, (int) oppositeTop ),
new Point( (int) adjacentTop, nHeight ),
new Point( (int) oppositeBottom, 0 )
};
}
else if (locked_theta >= Math.PI && locked_theta < (Math.PI + pi2))
{
points = new Point[] {
new Point( (int) adjacentTop, nHeight ),
new Point( 0, (int) adjacentBottom ),
new Point( nWidth, (int) oppositeTop )
};
}
else
{
points = new Point[] {
new Point( 0, (int) adjacentBottom ),
new Point( (int) oppositeBottom, 0 ),
new Point( (int) adjacentTop, nHeight )
};
}
//g.Clear(Color.Transparent);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.DrawImage(image, points);
}
return rotatedBmp;
}
MemoryStream MakeTransparent(MemoryStream origBitmapMemoryStream)
{
Color transparentColor = GetColor("White");
int transparentArgb = transparentColor.ToArgb();
using (Bitmap origBitmap = new Bitmap(origBitmapMemoryStream))
{
using (Bitmap newBitmap = new Bitmap(origBitmap.Width, origBitmap.Height, origBitmap.PixelFormat))
{
ColorPalette origPalette = origBitmap.Palette;
ColorPalette newPalette = newBitmap.Palette;
int index = 0;
int transparentIndex = -1;
foreach (Color origColor in origPalette.Entries)
{
newPalette.Entries[index] = Color.FromArgb(255, origColor);
if (origColor.ToArgb() == transparentArgb) transparentIndex = index;
index += 1;
}
if (-1 == transparentIndex)
{
return origBitmapMemoryStream;
}
newPalette.Entries[transparentIndex] = Color.FromArgb(0, transparentColor);
newBitmap.Palette = newPalette;
Rectangle rect = new Rectangle(0, 0, origBitmap.Width, origBitmap.Height);
BitmapData origBitmapData = origBitmap.LockBits(rect, ImageLockMode.ReadOnly, origBitmap.PixelFormat);
BitmapData newBitmapData = newBitmap.LockBits(rect, ImageLockMode.WriteOnly, newBitmap.PixelFormat);
for (int y = 0; y < origBitmap.Height; y++)
{
for (int x = 0; x < origBitmap.Width; x++)
{
byte origBitmapByte = Marshal.ReadByte(origBitmapData.Scan0, origBitmapData.Stride * y + x);
Marshal.WriteByte(newBitmapData.Scan0, newBitmapData.Stride * y + x, origBitmapByte);
}
}
newBitmap.UnlockBits(newBitmapData);
origBitmap.UnlockBits(origBitmapData);
MemoryStream m = new MemoryStream();
newBitmap.Save(m, ImageFormat.Gif);
return m;
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>