vs2005
控件DataGridView中的DataGridViewIamgeCell列中的一个单元格中既有图片又有文字怎么实现???
是否要重写DataGridViewIamgeCell类?

解决方案 »

  1.   

    把图片和文字绘图在一个bitmap里赋值给单元格
      

  2.   


    C# code
    #region IHttpHandler 成员    public bool IsReusable
        {
            get { return false; }
        }    public void ProcessRequest(HttpContext context)
        {
            string imagePath = context.Request.PhysicalPath;
            Bitmap image = null;
            if (context.Cache[imagePath] == null)
            {
                image = new Bitmap(imagePath);//实例化图片
                image = AddWaterMark(image);//给图片添加文字
                context.Cache[imagePath] = image;
            }
            else
            {
                image = context.Cache[imagePath] as Bitmap;
            }
            image.Save(context.Response.OutputStream, ImageFormat.Jpeg);//将添加水印的图片输入到当前流中
        }
            #endregion    //给图片添加水印
        private Bitmap AddWaterMark(Bitmap image)
        {
    //读取config文件中设置的给图片要添加的文字
            string text = System.Configuration.ConfigurationManager.AppSettings["WaterMark"].ToString();
    //读取配置中设置的添加文字的字体大小
            int fontSize = int.Parse(System.Configuration.ConfigurationManager.AppSettings["Font-Size"].ToString());
            Font font = new Font("宋体", fontSize);        Brush brush = Brushes.DarkGray;
            Graphics g = Graphics.FromImage(image);
    //获取用指定字体绘制指定字符串所需要的Size大小
            SizeF size = g.MeasureString(text, font);
    //在图片上绘制文字
            g.DrawString(text, font, brush, image.Width - size.Width, image.Height - size.Height);
            g.Dispose();
            return image;
        }
      

  3.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication44
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.Controls.Clear();            DataTable DT = new DataTable();
                DT.Columns.Add("image", typeof(Image));
                Bitmap Bmp = new Bitmap(100, 100); // 模拟数据里有张图片
                Graphics G = Graphics.FromImage(Bmp);
                G.FillRectangle(new SolidBrush(Color.Red), new Rectangle(new Point(0, 0), Bmp.Size));
                G.Dispose();
                DT.Rows.Add(new Object[] { Bmp });
                Bmp = new Bitmap(100, 100); // 模拟数据里有张图片
                G = Graphics.FromImage(Bmp);
                G.FillRectangle(new SolidBrush(Color.Green), new Rectangle(new Point(0, 0), Bmp.Size));
                G.Dispose();
                DT.Rows.Add(new Object[] { Bmp });            DataGridView DGV = new DataGridView();
                DGV.Parent = this;
                DGV.Dock = DockStyle.Fill;
                DGV.AllowUserToAddRows = false;
                DGV.AllowUserToDeleteRows = false;
                DataGridViewImageColumn IC = new DataGridViewImageColumn();
                IC.DataPropertyName = "image";
                DGV.Columns.Add(IC);
                DGV.CellFormatting += new DataGridViewCellFormattingEventHandler(DGV_CellFormatting);
                DGV.DataSource = DT;
            }        void DGV_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (e.ColumnIndex == 0 && e.Value != DBNull.Value)
                {
                    ((DataGridView)sender).Rows[e.RowIndex].Height = 100;
                    Bitmap Bmp = (Bitmap)e.Value;
                    Bitmap NewBmp = new Bitmap(100, 150);
                    Graphics G = Graphics.FromImage(NewBmp);
                    G.DrawImage(Bmp, new Point(0, 0));
                    G.DrawString("图片" + (e.RowIndex + 1).ToString(), new Font("宋体", 10), new SolidBrush(Color.Black), new PointF(0, 101));
                    G.Dispose();
                    e.Value = NewBmp;
                }
            }
        }
    }
      

  4.   

    这个不改变原图,如果是要改变原图,要修改datatable而不是利用DataGridViewCellFormatting