DataGridView中DataGridViewImageCell图片列问题!!! vs2005控件DataGridView中的DataGridViewIamgeCell列中的一个单元格中既有图片又有文字怎么实现???是否要重写DataGridViewIamgeCell类? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把图片和文字绘图在一个bitmap里赋值给单元格 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; } 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; } } }} 这个不改变原图,如果是要改变原图,要修改datatable而不是利用DataGridViewCellFormatting 注册时弹出子窗口,后面的主窗口不能操作 file html控件 C# 打开超大tif文件文件的问题 请问这个的结果是多少,为什么???? 放大和缩小bmp图片怎样不失真? ~~~~~~~~~~~~~~关于网络端口的一个小问题~~~~~~~~~~~~~~ 请问VS2012跑起来的WEB API如何在其他机器访问 怎么看窗体已经显示? 当我在主窗体调用子窗体时(一定要用ShowDialog),窗体显示后立即退出。退出代码在哪写 大送分,答对全给你 数组求最大值无效(苦恼!) 在c#中怎样在datagridview行首实现编号
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;
}
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;
}
}
}
}