2015-09-17 223 views
-4

我想保存圖像中充滿紅色的長方形,但我得到了一個文件,該文件不是一個圖像,這是我的代碼:保存圖像

protected void btnDrowRect_Click(object sender, EventArgs e) 
    { 

    string fn1 = Session["WorkingImage"].ToString(); 

    string imagePath = Server.MapPath("~/Images/rec" + fn1); 
    string savetoo = Server.MapPath("~/Images/rect/rec" + DateTime.Now.ToString("hhmmss")); 
    string imageNewPath = savetoo; ; 

    Image image = Image.FromFile(imagePath); 

    int a = Int32.Parse(imgw.Value)/2 - img3.Width/2; 
    int b = Int32.Parse(imgh.Value)/2 - img3.Height/2; 

    Rectangle srcRect = new Rectangle(-a, -b, Int32.Parse(imgw.Value),Int32.Parse(imgh.Value)); 
    Bitmap bmp = new Bitmap(Int32.Parse(imgw.Value), Int32.Parse(imgh.Value)); 
    using (Graphics g = Graphics.FromImage(bmp)) 
    { 
     // Create image. 
     Image newImage = Image.FromFile(imagePath); 

     // Create rectangle for displaying image. 
     Rectangle destRect = new Rectangle(0, 0, Int32.Parse(imgw.Value), Int32.Parse(imgh.Value)); 

     // Create rectangle for source image. 

     GraphicsUnit units = GraphicsUnit.Pixel; 

     Color customColor = Color.FromArgb(100, Color.Red); 
     SolidBrush shadowBrush = new SolidBrush(customColor); 
     g.FillRectangles(shadowBrush, new RectangleF[] { destRect }); 

     // Draw image to screen. 
     g.DrawImage(newImage, destRect, srcRect, units); 
     // g.Save(); 

    } 

    bmp.Save(imageNewPath); 

} 

點擊此處查看所保存的文件:

http://i.stack.imgur.com/Nso3c.png

但我想保存這樣的:

http://i.stack.imgur.com/B7CUl.png

+1

'http://i.stack.imgur.com/Nso3c .png'是borken。 –

+0

int a = Int32.Parse(imgw.Value)/ 2 - img3.Width/2; int b = Int32.Parse(imgh.Value)/ 2 - img3.Height/2; 什麼是imgh,imgw和img3?它沒有被定義在哪裏 –

+1

它們是完全不同的圖像..究竟是什麼'像這樣'指定?圖像錯了?大小錯誤?你想要粉紅色的邊框?不要只是轉儲含有模糊文字的代碼,並期望人們弄清楚你需要什麼...... – Rob

回答

0

你可以嘗試

string savetoo = Server.MapPath("~/Images/rect/rec" + DateTime.Now.ToString("hhmmss") + ".jpg"); 

bmp.Save(imageNewPath, ImageFormat.Jpeg); 

,這將是工作