C# 生成图片与文字组合图片

var width = baseSite.Name.Length * 39;
var height = 62;
var domain = baseSite.SiteDomain.SplistToList(",").FirstOrDefault();

Font font = new System.Drawing.Font("华文行楷", 28, (FontStyle.Bold));
Brush backBrush = Brushes.DarkGoldenrod;
Brush foreBrush = Brushes.Gold;

var filePath = (RootPath + @"images").RandomFile();
Image watermark = new Bitmap(filePath);
var imgHeight = 50;
var imgWidth = (int)Math.Ceiling((imgHeight / (double)watermark.Height) * watermark.Width);

width = width + imgWidth + 6;
Bitmap image = new Bitmap(width, height);
Graphics g = Graphics.FromImage(image);

try
{
    Single strPosX = imgWidth + 3;
    Single posY = 4;
    g.Clear(Color.Transparent);
    g.DrawString(baseSite.Name, font, foreBrush, strPosX, posY);
    g.DrawString(domain.ToUpper(), new System.Drawing.Font("宋体", 15, (FontStyle.Bold)), backBrush, strPosX + 8, 38);
    g.DrawImage(watermark, new Rectangle(6, 6, imgWidth, imgHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, null);
    g.InterpolationMode = InterpolationMode.Default;
    g.SmoothingMode = SmoothingMode.Default;
    g.PixelOffsetMode = PixelOffsetMode.Default;
    g.CompositingMode = CompositingMode.SourceOver;
    g.CompositingQuality = CompositingQuality.Default;
    g.TextRenderingHint = TextRenderingHint.SystemDefault;
    g.PageUnit = GraphicsUnit.Pixel;

    MemoryStream ms = new MemoryStream();
    image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    byte[] bytes = ms.GetBuffer();
    Response.Clear();
    Response.ContentType = "image/png";
    Response.BinaryWrite(bytes);
    Response.End();
}
finally
{
    g.Dispose();
    watermark.Dispose();
}