2012-07-30 66 views
1

當我按下保存按鈕時,我只能保存文本而不是JPEG格式的圖像。如何將圖片和文本保存爲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圖像中,我們只保存html格式的圖像源信息,而不是保存爲jpeg格式時的原始圖像。

+0

'DrawString'不支持HTML,因此它將始終呈現爲文本。 – 2012-07-30 12:13:04

+0

@Richard Szalay那麼我需要使用哪種方法來保存圖片以及文字 – Anish 2012-07-30 12:13:53

回答

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和...

正則表達式(正則表達式),幫助您獲得:

  • 標籤
  • 樣式
  • 從你的內容

所以,你想每一個東西,閱讀正則表達式教程

你必須安排最終圖像的用自己的計算內容。 祝你好運...

+0

感謝您的補救措施:-)如何使圖片對齊完美......它在輸出中顯示了我的大尺寸。 – Anish 2012-07-31 04:30:09

+0

感謝您的補救:-)如何使圖片對齊完美......它顯示了我在輸出的大尺寸。我在HTML(高度和寬度)中給出的尺寸應該保存在輸出中,而不是爲我輸入。 – Anish 2012-07-31 04:36:08

+0

我在答案的底部添加了新行... – RAM 2012-07-31 13:18:47

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); 
+0

謝謝............................. – Anish 2012-08-01 04:20:04