using System;

namespace KuCun
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "{0}                          {1}         {2}";

            BuildImage buildImage = ImageProvider.Provider;

            for (int i = 0; i < 10; i++)
            {

                System.Threading.Thread.Sleep(500);

                ImageInfo imageInfo = buildImage.GenerateImage(string.Format(str, "100", DateTime.Now.ToString(), "丰台库"), System.Drawing.Image.FromFile(@"C:\Documents and Settings\Administrator\桌面\KuCun\kucun.gif"));

                imageInfo.Image.Save(DateTime.Now.Ticks.ToString() + ".jpg");
            }
        }
    }
}

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

namespace KuCun
{
    /// <summary>
    /// 图片提供程序
    /// </summary>
    public static class ImageProvider
    {
        private static BuildImage buildImage = null;

        /// <summary>
        /// 生成图片
        /// </summary>
        public static BuildImage Provider
        {
            get
            {
                if (buildImage == null)
                {
                    buildImage = new BuildImage();
                }
                return buildImage;
            }
        }
    }

    public class BuildImage
    {
        private static Matrix m = new Matrix();

        // 定义字体
        private static Font[] fonts = {
                                        new Font(new FontFamily("Times New Roman"), 18, FontStyle.Bold),
                                        new Font(new FontFamily("Georgia"), 18 , FontStyle.Bold),
                                        new Font(new FontFamily("Helvetica"), 18, FontStyle.Bold),
                                        new Font(new FontFamily("Arial"), 18 , FontStyle.Bold)
                                     };

        /// <summary>
        /// 生成图片
        /// </summary>
        public ImageInfo GenerateImage(string str, Image bgImg)
        {
            ImageInfo imageInfo = new ImageInfo();
            imageInfo.ImageFormat = ImageFormat.Jpeg;
            imageInfo.ContentType = "image/pjpeg";

            Bitmap bitmap = new Bitmap(bgImg, bgImg.Width, bgImg.Height);
            Graphics g = Graphics.FromImage(bitmap);

            // 定义前景色 字体颜色
            SolidBrush drawBrush = new SolidBrush(Color.Blue);

            // 写字
            g.DrawString(str, fonts[3], drawBrush, new PointF(100, 12));

            g.ResetTransform();

            drawBrush.Dispose();
            g.Dispose();
            g.Dispose();

            imageInfo.Image = bitmap;

            return imageInfo;
        }
    }

    /// <summary>
    /// 图片信息
    /// </summary>
    public class ImageInfo
    {
        private Bitmap image;
        private string contentType = "image/pjpeg";
        private ImageFormat imageFormat = ImageFormat.Jpeg;

        /// <summary>
        /// 生成的图片
        /// </summary>
        public Bitmap Image
        {
            get { return image; }
            set { image = value; }
        }

        /// <summary>
        /// 图片类型,如 image/pjpeg
        /// </summary>
        public string ContentType
        {
            get { return contentType; }
            set { contentType = value; }
        }

        /// <summary>
        /// 图片格式
        /// </summary>
        public ImageFormat ImageFormat
        {
            get { return imageFormat; }
            set { imageFormat = value; }
        }
    }
}

posted on 2011-02-18 16:13  高兴happy  阅读(2024)  评论(0编辑  收藏  举报