如何将图片和文本保存到jpeg中

时间:2012-07-30 12:07:16

标签: c# asp.net c#-4.0 c#-3.0

当我按下保存按钮时,我只能保存JPEG格式的图像而不是图像。

HTML

  <div id="painter" runat="server" style="left: 280px; position: absolute; top: 82px;
    width: 311px; height: 388px; background-color: #ccccff;">
    hi niladri
    <br />
    helo
    <br />
    dvshfgjfhglkglkhjlhkjmlkhklfhfg sasasasa
    <br />
    sssdsdsds


    <img src="image/1.jpg" alt="Osr" height="100" width="309" />
            </div>

<asp:Button ID="Save" runat="server" Style="left: 733px; position: absolute; top: 266px"
    Text="Button" OnClick="Save_Click" />

C#代码:

  public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Save_Click(object sender, EventArgs e)
{
          string[] value = painter.Style.Value.Split(';');
    string hw = painter.InnerHtml;
    hw = hw.Replace("<br />", Environment.NewLine);

    string width = value[3];
    string height = value[4];
    string bgcolor = value[5];

    string[] widthArray = width.Split(':');
    string[] heightArray = height.Split(':');
    string[] bgcolorArray = bgcolor.Split(':');

    int w = int.Parse(widthArray[1].Replace("px", ""));
    int h = int.Parse(heightArray[1].Replace("px", ""));

    string color = bgcolorArray[1];

    //var overlay = new Bitmap(imagePath2);

    System.Drawing.Color c = System.Drawing.Color.Cyan;//.FromName("#ccccff");

    System.Drawing.Bitmap bt = new System.Drawing.Bitmap(w, h);
    System.Drawing.Graphics oGraphics = System.Drawing.Graphics.FromImage(bt);
    System.Drawing.Brush brush = new System.Drawing.SolidBrush(c);
    oGraphics.FillRectangle(brush, 0, 0, w, h);
    oGraphics.DrawString(hw, new Font("Arial", 12, FontStyle.Italic), SystemBrushes.WindowText, new PointF(50, 50));

    bt.Save("E:\\image10.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);  

 }
 }

图片:

在附加的Jpeg图像中,当我们保存为jpeg格式时,我们只得到html图像源信息而不是原始图像。

3 个答案:

答案 0 :(得分:2)

使用以下代码:

protected void Save_Click(object sender, EventArgs e)
{
        string[] value = painter.Style.Value.Split(';');
        string hw = painter.InnerHtml;
        hw = hw.Replace("<br />", Environment.NewLine);

        string width = value[3];
        string height = value[4];
        string bgcolor = value[5];

        string[] widthArray = width.Split(':');
        string[] heightArray = height.Split(':');
        string[] bgcolorArray = bgcolor.Split(':');

        int w = int.Parse(widthArray[1].Replace("px", ""));
        int h = int.Parse(heightArray[1].Replace("px", ""));
        //=====================================================================
        //Added By RAM:
        string key = "src=";
        int start_of_img_address = hw.IndexOf(key) + key.Length+1;
        int end_of_img_address = hw.Substring(start_of_img_address + 1).IndexOf('"');
        string image_addres = hw.Substring(start_of_img_address, end_of_img_address+1);
        image_addres = Server.MapPath(image_addres);
        Image my_img = Image.FromFile(image_addres);

        key = "<img";
        int start_of_img_tag = hw.IndexOf(key);
        int end_of_img_tag = hw.Substring(start_of_img_tag).IndexOf('>')+1;
        string img_tag = hw.Substring(start_of_img_tag, end_of_img_tag+1);
        hw=hw.Remove(start_of_img_tag, end_of_img_tag);
        Response.Write(img_tag);

        //=====================================================================
        string color = bgcolorArray[1];

        //var overlay = new Bitmap(imagePath2);

        System.Drawing.Color c = System.Drawing.Color.Cyan; //.FromName("#ccccff");

        System.Drawing.Bitmap bt = new System.Drawing.Bitmap(w, h);
        System.Drawing.Graphics oGraphics = System.Drawing.Graphics.FromImage(bt);
        System.Drawing.Brush brush = new System.Drawing.SolidBrush(c);
        oGraphics.FillRectangle(brush, 0, 0, w, h);
        oGraphics.DrawString(hw, new Font("Arial", 12, FontStyle.Italic), SystemBrushes.WindowText, new PointF(0, 0)); //changed 50 to 0

        //=====================================================================
        //Added By RAM:
        oGraphics.DrawImage(my_img, 0, 170, my_img.Width, my_img.Height);
        //=====================================================================

        bt.Save("E:\\image10.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}

...和结果:

enter image description here

修改

您也可以使用正则表达式代替子串,IndexOf 和...

正则表达式(RegEx)可帮助您获取:

  • 标签
  • 样式
  • 您想要的所有内容

所以,请阅读RegEx教程

您必须使用自己的计算排列最终图像的内容。 祝你好运......

答案 1 :(得分:0)

您只获取div的InnerHTML(只会获取文本),因此将忽略IMG标记。如果要在Web源中指定图像的路径,可以使用服务器控件(Image)并从中获取图像路径。将图像加载到后面的代码中并将其绘制到Bitmap对象,然后覆盖文本并保存。

int w = 640;
int h = 480;

System.Drawing.Color c = System.Drawing.Color.White;

string imagePath = Server.MapPath("~/image/1.jpg"); // here you would get the image source of your Image control
Image img = Image.FromFile(imagePath);

System.Drawing.Bitmap bt = new System.Drawing.Bitmap(w, h);
System.Drawing.Graphics oGraphics = System.Drawing.Graphics.FromImage(bt);
System.Drawing.Brush brush = new System.Drawing.SolidBrush(c);
oGraphics.FillRectangle(brush, 0, 0, w, h);
oGraphics.DrawImage(img, 0, 0, img.Width, img.Height);
oGraphics.DrawString("this is some text", new Font("Arial", 12, FontStyle.Italic), SystemBrushes.WindowText, new PointF(50, 50));

bt.Save("c:\\image10.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

答案 2 :(得分:0)

在调用DrawString之前使用Graphics.DrawImage。

Graphics.DrawImage(Image.FromFile(Server.MapPath("~/image/1.jpg")), 0, 0, w, h);

Graphics.DrawImage:http://msdn.microsoft.com/en-us/library/dbsak4dc.aspx