在HTML标记中使用System.Drawing.Image [英] Use a System.Drawing.Image in an HTML tag

查看:134
本文介绍了在HTML标记中使用System.Drawing.Image的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个潜在的问题。我有一个图像存储在数据库中,然后在我的应用程序中使用C#方法来获取该图像并将其存储在自定义类中,如下所示:

  public class MyImage 
{
public System.Drawing.Image myImageFromDB;
public string imageName;
public int imageNumberInCollection;
}

我的问题是:我可以/如何使用此图片类的HTML图像标签?我已经尝试了以下内容,但它只是返回一个带有红色X的框:

  // myImageFromDBCollection是MyImage列表对象
foreach(var ind in myImagesFromDBCollection)
{
table + = String.Format(< image src = {0} />,ind.myImageFromDB);
}

任何关于我在做什么的想法都是错误的?

$ b $我最终用Brads方法来解决这个问题(请参阅问题的评论)我最终创建了一个方法,它接受了一个System.Drawing。图像并将其转换为字节数组,然后对该字节数组进行编码以获取图像。代码如下所示:

  byte [] imgBytes = turnImageToByteArray(ind.ind.myImageFromDB); 
string imgString = Convert.ToBase64String(imgBytes);
table + = String.Format(img src = \data:image / Bmp; base64,{0} \>,imgString);

private byte [] turnImageToByteArray(System.Drawing.Image img)
{
MemoryStream ms = new MemoryStream();
img.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
返回ms.ToArray();

$ / code>

目前这适用于我的目的,非常感谢大家和他们的建议,总是每个人都来,并帮助:-)


I have a potentially easy question. I have an image stored in a database, I then used a C# method in my application that gets that image and stores it in a custom class that looks like this:

public class MyImage
{
 public System.Drawing.Image myImageFromDB;
 public string imageName;
 public int imageNumberInCollection;
}

My question is this: Can I/How can I use the image from this class in an HTML image tag? I have attempted the following but it just returns a box with a red X in it:

//myImageFromDBCollection is a list of MyImage objects
foreach(var ind in myImagesFromDBCollection)
{
  table += String.Format("<image src={0} />", ind.myImageFromDB);
}

Any ideas on what I am doing wrong?

解决方案

I ended up going with Brads Method of solving this (See the comment under the question) I ended up creating a method that takes in a System.Drawing.Image and converts it to a byte array then encode that byte array to get the image. The code looks like the following:

byte[] imgBytes = turnImageToByteArray(ind.ind.myImageFromDB);
string imgString = Convert.ToBase64String(imgBytes);
table += String.Format("img src=\"data:image/Bmp;base64,{0}\">", imgString);

private byte[] turnImageToByteArray(System.Drawing.Image img)
{
  MemoryStream ms = new MemoryStream();
  img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
  return ms.ToArray();
}

Currently this works for my purposes so thank you to everyone and their suggestions, as always everyone comes though and helps :-)

这篇关于在HTML标记中使用System.Drawing.Image的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆