动态生成图片。类似验证码

liuleiyu121520 2011-03-23 01:00:58
HtmlTable table_House = new HtmlTable();
if (feild!=null)
{
table_House.Style.Add("width", "100%");
table_House.Style.Add("cellpadding", "0px");
table_House.Style.Add("cellpadding", "0px");
for (int i = 0; i < Fieldhouse.Length; i++)
{

HtmlTableRow tr = new HtmlTableRow();

HtmlTableCell tdName = new HtmlTableCell();
tdName.InnerText = Fieldhouse[i].fieldName;
tdName.Style.Add("text-align", "center");
tdName.Style.Add("width", "50%");
tdName.Style.Add("border-bottom", "solid 2px #E5E5E5");
tdName.Style.Add("border-right", "solid 2px #E5E5E5");
HtmlTableCell tdValue = new HtmlTableCell();
tdValue.InnerText = Fieldhouse[i].fieldValue;
tdValue.Style.Add("text-align", "center");
tdValue.Style.Add("width", "50%");
tdValue.Style.Add("border-bottom", "solid 2px #E5E5E5");
tr.Cells.Add(tdName);
tr.Cells.Add(tdValue);
table_House.Rows.Add(tr);

}
}
int heigth=feild.Length*20;
System.Drawing.Bitmap img = new System.Drawing.Bitmap(heigth, 790);
using (System.Drawing.Graphics g =System.Drawing.Graphics.FromImage(img))
{
g.Clear(System.Drawing.Color.White);

}

我动态生成了一个表格并且绑定了数据。也生成了一个图片。我想把这个表格写入到这个图片(img)中。并显示到页面上。哪位大哥能教教
...全文
166 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
le616 2011-03-23
  • 打赏
  • 举报
回复
1.动态的生成图片 (生成xy坐标),可参考生成曲线图,饼图,柱状图。
参考
asp.net的生成曲线图的过程
2.使用第三方控件。WebChart ,ZedGraph
参考
WebChart
ZedGraph
liuleiyu121520 2011-03-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 le616 的回复:]
表格写入到这个图片(img)中???
还是图片显示在表格中。。。
[/Quote]
就是把表格的数据以及边框什么的都写入到动态生成的那个img中。然后显示在页面上
le616 2011-03-23
  • 打赏
  • 举报
回复
表格写入到这个图片(img)中???
还是图片显示在表格中。。。
liuleiyu121520 2011-03-23
  • 打赏
  • 举报
回复
那个field数组中有两个字段。就是要画到图片上是2列多行。行是根据数组的长度而定的
liuleiyu121520 2011-03-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wxr0323 的回复:]
C# code
//定义图像的大小,生成图像的实例
Img = new Bitmap(100, 25);

g = Graphics.FromImage(Img);//从Img对象生成新的Graphics对象

g.Clear(Color.White);//背景设为白色

for (int i = 0; i < ……
[/Quote]
那个是数组。哪有substring方法
liuleiyu121520 2011-03-23
  • 打赏
  • 举报
回复
大哥们。我要动态生成额。全部都要动态生成。麻烦教教小弟啦
子夜__ 2011-03-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fengyarongaa 的回复:]

C# code
/// <summary>
/// 文字水印
/// </summary>
/// <param name="imgPath">服务器图片相对路径</param>
/// <param name="filename">保存文件名</param>
/// <param name="watermarkT……
[/Quote]
水印怎么都整上了。
ycproc 2011-03-23
  • 打赏
  • 举报
回复
/// <summary>
/// 文字水印
/// </summary>
/// <param name="imgPath">服务器图片相对路径</param>
/// <param name="filename">保存文件名</param>
/// <param name="watermarkText">水印文字</param>
/// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param>
/// <param name="quality">附加水印图片质量,0-100</param>
/// <param name="fontname">字体</param>
/// <param name="fontsize">字体大小</param>
public static void AddImageSignText(string imgPath, string filename, string watermarkText, int watermarkStatus, int quality, string fontname, int fontsize)
{
byte[] _ImageBytes = File.ReadAllBytes(HttpContext.Current.Server.MapPath(imgPath));
Image img = Image.FromStream(new System.IO.MemoryStream(_ImageBytes));
filename = HttpContext.Current.Server.MapPath(filename);

Graphics g = Graphics.FromImage(img);
Font drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel);
SizeF crSize;
crSize = g.MeasureString(watermarkText, drawFont);

float xpos = 0;
float ypos = 0;

switch (watermarkStatus)
{
case 1:
xpos = (float)img.Width * (float).01;
ypos = (float)img.Height * (float).01;
break;
case 2:
xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
ypos = (float)img.Height * (float).01;
break;
case 3:
xpos = ((float)img.Width * (float).99) - crSize.Width;
ypos = (float)img.Height * (float).01;
break;
case 4:
xpos = (float)img.Width * (float).01;
ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
break;
case 5:
xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
break;
case 6:
xpos = ((float)img.Width * (float).99) - crSize.Width;
ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
break;
case 7:
xpos = (float)img.Width * (float).01;
ypos = ((float)img.Height * (float).99) - crSize.Height;
break;
case 8:
xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
ypos = ((float)img.Height * (float).99) - crSize.Height;
break;
case 9:
xpos = ((float)img.Width * (float).99) - crSize.Width;
ypos = ((float)img.Height * (float).99) - crSize.Height;
break;
}

g.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1);
g.DrawString(watermarkText, drawFont, new SolidBrush(Color.Black), xpos, ypos);

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType.IndexOf("jpeg") > -1)
ici = codec;
}
EncoderParameters encoderParams = new EncoderParameters();
long[] qualityParam = new long[1];
if (quality < 0 || quality > 100)
quality = 80;

qualityParam[0] = quality;

EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
encoderParams.Param[0] = encoderParam;

if (ici != null)
img.Save(filename, ici, encoderParams);
else
img.Save(filename);

g.Dispose();
img.Dispose();
}
}
ycproc 2011-03-23
  • 打赏
  • 举报
回复

public class ImageWaterMark
{
/// <summary>
/// 图片水印
/// </summary>
/// <param name="imgPath">服务器图片相对路径</param>
/// <param name="filename">保存文件名</param>
/// <param name="watermarkFilename">水印文件相对路径</param>
/// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param>
/// <param name="quality">附加水印图片质量,0-100</param>
/// <param name="watermarkTransparency">水印的透明度 1--10 10为不透明</param>
public static void AddImageSignPic(string imgPath, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency)
{
byte[] _ImageBytes = File.ReadAllBytes(HttpContext.Current.Server.MapPath(imgPath));
Image img = Image.FromStream(new System.IO.MemoryStream(_ImageBytes));
filename = HttpContext.Current.Server.MapPath(filename);

if (watermarkFilename.StartsWith("/") == false)
watermarkFilename = "/" + watermarkFilename;
watermarkFilename = HttpContext.Current.Server.MapPath(watermarkFilename);
if (!File.Exists(watermarkFilename))
return;
Graphics g = Graphics.FromImage(img);
//设置高质量插值法
//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
//g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Image watermark = new Bitmap(watermarkFilename);

if (watermark.Height >= img.Height || watermark.Width >= img.Width)
return;

ImageAttributes imageAttributes = new ImageAttributes();
ColorMap colorMap = new ColorMap();

colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
ColorMap[] remapTable = { colorMap };

imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

float transparency = 0.5F;
if (watermarkTransparency >= 1 && watermarkTransparency <= 10)
transparency = (watermarkTransparency / 10.0F);


float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, transparency, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};

ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

int xpos = 0;
int ypos = 0;

switch (watermarkStatus)
{
case 1:
xpos = (int)(img.Width * (float).01);
ypos = (int)(img.Height * (float).01);
break;
case 2:
xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
ypos = (int)(img.Height * (float).01);
break;
case 3:
xpos = (int)((img.Width * (float).99) - (watermark.Width));
ypos = (int)(img.Height * (float).01);
break;
case 4:
xpos = (int)(img.Width * (float).01);
ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
break;
case 5:
xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
break;
case 6:
xpos = (int)((img.Width * (float).99) - (watermark.Width));
ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
break;
case 7:
xpos = (int)(img.Width * (float).01);
ypos = (int)((img.Height * (float).99) - watermark.Height);
break;
case 8:
xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
ypos = (int)((img.Height * (float).99) - watermark.Height);
break;
case 9:
xpos = (int)((img.Width * (float).99) - (watermark.Width));
ypos = (int)((img.Height * (float).99) - watermark.Height);
break;
}

g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType.IndexOf("jpeg") > -1)
ici = codec;
}
EncoderParameters encoderParams = new EncoderParameters();
long[] qualityParam = new long[1];
if (quality < 0 || quality > 100)
quality = 80;

qualityParam[0] = quality;

EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
encoderParams.Param[0] = encoderParam;

if (ici != null)
img.Save(filename, ici, encoderParams);
else
img.Save(filename);

g.Dispose();
img.Dispose();
watermark.Dispose();
imageAttributes.Dispose();
}

子夜__ 2011-03-23
  • 打赏
  • 举报
回复
 //定义图像的大小,生成图像的实例
Img = new Bitmap(100, 25);

g = Graphics.FromImage(Img);//从Img对象生成新的Graphics对象

g.Clear(Color.White);//背景设为白色

for (int i = 0; i < VNum.Length; i++)
{
int cindex = random.Next(7);
int findex = random.Next(5);
Font f = new System.Drawing.Font(fonts[findex], 14, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(c[cindex]);
int ii = 4;

g.DrawString(VNum.Substring(i, 1), f, b, 3 + (i * 20), ii);
}
ms = new MemoryStream();//生成内存流对象
Img.Save(ms, ImageFormat.Jpeg);//将此图像以jpg图像文件的格式保存到流中

//回收资源
g.Dispose();
Img.Dispose();

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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