2012-07-30 78 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); 
    ... 
}