2012-07-30 75 views
-1

我想創建一個由兩個或多個圖像組成的圖像。基本上我有一個網址列表 ,我需要使用這些URL來生成一個圖像(使用URL導向的圖像)我試過使用:Combining Images with C#。然而,我似乎無法得到它的工作。我得到的錯誤是參數無效?連接圖像

編輯:

這裏是我使用此刻的代碼,文件,是文件路徑的字符串格式的數組。

public static System.Drawing.Bitmap Combine(string[] files) 
{ 
    //read all images into memory 
    List<System.Drawing.Bitmap> images = new List<System.Drawing.Bitmap>(); 
    System.Drawing.Bitmap finalImage = null; 

try 
    { 
    int width = 0; 
    int height = 0; 

    foreach (string image in files) 
    { 
     //create a Bitmap from the file and add it to the list 
     System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image); 
     //FUNCTION FAILUURE HERE 

     //update the size of the final bitmap 
     width += bitmap.Width; 
     height = bitmap.Height > height ? bitmap.Height : height; 

     images.Add(bitmap); 
    } 

    //create a bitmap to hold the combined image 
    finalImage = new System.Drawing.Bitmap(width, height); 

    //get a graphics object from the image so we can draw on it 
    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage)) 
    { 
     //set background color 
     g.Clear(System.Drawing.Color.Black); 

     //go through each image and draw it on the final image 
     int offset = 0; 
     foreach (System.Drawing.Bitmap image in images) 
     { 
     g.DrawImage(image, 
      new System.Drawing.Rectangle(offset, 0, image.Width, image.Height)); 
     offset += image.Width; 
     } 
    } 

    return finalImage; 
    } 
    catch(Exception ex) 
    { 
    if (finalImage != null) 
    finalImage.Dispose(); 

    throw ex; 
    } 
    finally 
    { 
    //clean up memory 
    foreach (System.Drawing.Bitmap image in images) 
    { 
     image.Dispose(); 
    } 
    } 
} 

我使用這個函數來創建位圖圖像,然後我保存此位圖圖像,然後使用URL來顯示位圖圖像。然而,功能無法創建在點關口圖像中的代碼(由Mark://功能失效HERE)

錯誤消息:

Server Error in '/' Application.

Parameter is not valid.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Parameter is not valid.

Source Error:

Line 75: finalImage.Dispose(); Line 76: Line 77: throw ex; Line 78: } Line 79: finally

Source File: C:\Documents and Settings\sc541856\My Documents\Visual Studio 2010\Projects\Pontoon\Pontoon\Interaction.aspx.cs Line: 77

Stack Trace:

[ArgumentException: Parameter is not valid.]
Pontoon.Interfaction.Combine(String[] files) in C:\Documents and Settings\sc541856\My Documents\Visual Studio 2010\Projects\Pontoon\Pontoon\Interaction.aspx.cs:77
Pontoon.Interfaction.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\sc541856\My Documents\Visual Studio 2010\Projects\Pontoon\Pontoon\Interaction.aspx.cs:24
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

+4

您好。嘗試顯示一些_relevant_和_brief_代碼來重現您的問題,以及您遇到錯誤的哪一行。否則,這只是猜測。 =) – 2012-07-30 10:54:25

+1

併發布*精確*錯誤消息,複製+粘貼。 – 2012-07-30 10:55:47

+0

您傳遞無效參數。這就是我們所能告訴你的。請添加更多詳細信息:代碼,獲取錯誤的行,錯誤消息或堆棧跟蹤 – Reniuz 2012-07-30 10:56:41

回答

0

這構造函數需要的位圖文件路徑,而不是URL 。您可以將文件下載到臨時文件夾中,如下所示:

WebClient client = new WebClient(); 
foreach (string image in files) 
{ 
    String imagePath = Path.GetTempFileName(); 
    client.DownloadFile(image, imagePath); 

    //create a Bitmap from the file and add it to the list 
    System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(imagePath); 
    ... 
}