在asp.net中将三个图像组合成单个图像

时间:2016-12-15 07:05:48

标签: c# html css asp.net image

在我的网络应用程序中,我需要将3个图像组合成单个图像(垂直对齐)图像。我有两个图像的代码到单个图像,但它们并排合并。我需要上下,以及如何添加第三张图片。

我在网上找到的代码:

我尝试了2张图片的代码:

的.aspx:

<div>
    <asp:Button ID="Button1" runat="server" Text="Merge" onclick="Button1_Click"/>    
    <asp:Image ID="Image1" runat="server"  ImageUrl="~/Images/Flower2.jpg"  Height="129px" Width="210px"/><br />
    <asp:Image ID="image2" runat="server" ImageUrl="~/Images/Flower4.jpg"  Height="129px" Width="210px"/><br />       
    <asp:Image ID="MergedCombinedImage" runat="server" />
</div>

的.cs

protected void Button1_Click(object sender, EventArgs e)
{
    string img1path = MapPath("~/Images/Flower2.jpg");
    string img2path = MapPath("~/Images/Flower4.jpg");         

    // Load two Images to be combined into Image Objects

    System.Drawing.Image img1= System.Drawing.Image.FromFile(img1path);
    System.Drawing.Image img2= System.Drawing.Image.FromFile(img2path);

    // Create a Resultant Image that’ll hold the above two Images
    //Here i am creating the final image with width as combined width of img1 and img2 and height as largest height among img1 and img2

    using (Bitmap FinalBitmap = new Bitmap(img1.Width + img2.Width, img2.Height > img1.Height ? img2.Height : img1.Height)) //This condition how can i add img3 
    {
        using (Graphics FinalImage = Graphics.FromImage(FinalBitmap))
        {
            // Draw the first image staring at point (0,0) with actual width and height of the image, in final image
            FinalImage.DrawImage(img1, new Rectangle(0, 0, img1.Width, img1.Height));                 
            // and Draw the second image staring at point where first image ends in the final image and save changes
            FinalImage.DrawImage(img2, img1.Width, 0);
            FinalImage.Save();
            // Write the bitmap to an image file and you’re done
            FinalBitmap.Save(MapPath("~/ResultImages/Outputimg.jpg"));
            MergedCombinedImage.ImageUrl = "~/ResultImages/Outputimg.jpg";
        }
    }
}

1 个答案:

答案 0 :(得分:0)

嗯,你有的代码是并排合并它。实际上,所有这些都是。我猜你自己没有写,但只是从某个地方抓住它。你真正需要做的就是在逻辑上做一个小的交换,你就完成了:

  • 您实例化的新位图是两个宽度的总和,并选择两个图像中的最大高度。您需要将其换成最大高度和最大宽度的总和。
  • 然后您将第一张图片绘制为完整尺寸。这可以不受影响。
  • 然后继续在X处绘制第二个图像,该图像对应于第一个图像结束的位置。相反,您应该将第二个图像的起始X坐标设置为0,将Y坐标设置为第一个图像的高度。
相关问题