DataGridView中DataGridViewImageCell图片列问题!!!

修改一下昵称 2009-04-29 04:16:32
vs2005
控件DataGridView中的DataGridViewIamgeCell列中的一个单元格中既有图片又有文字怎么实现???
是否要重写DataGridViewIamgeCell类?
...全文
1064 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiweihero 2009-04-29
  • 打赏
  • 举报
回复
image/*.jpg(等)
修改一下昵称 2009-04-29
  • 打赏
  • 举报
回复
四楼的实现定义了接口吗?
麻子Mozart 2009-04-29
  • 打赏
  • 举报
回复
帮顶
wartim 2009-04-29
  • 打赏
  • 举报
回复
这个不改变原图,如果是要改变原图,要修改datatable而不是利用DataGridViewCellFormatting
wartim 2009-04-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yhb417 的回复:]
引用 1 楼 wartim 的回复:
把图片和文字绘图在一个bitmap里赋值给单元格

怎么重绘,能给个列题吗?
谢谢!!
[/Quote]

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;
}
}
}
}
蓝海D鱼 2009-04-29
  • 打赏
  • 举报
回复

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;
}


修改一下昵称 2009-04-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wartim 的回复:]
把图片和文字绘图在一个bitmap里赋值给单元格
[/Quote]
怎么重绘,能给个列题吗?
谢谢!!
dhb008 2009-04-29
  • 打赏
  • 举报
回复
友情路过 帮顶
wartim 2009-04-29
  • 打赏
  • 举报
回复
把图片和文字绘图在一个bitmap里赋值给单元格

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧