2012-01-06 58 views
1

我可以从存储在数据库中的字节数组在服务器上创建映像。但我怎样将每个字节数组合成一个图像。基本上我想堆叠它们(它们都是1366px宽度和618px高度),然后将其保存为PNG图像。然后,我将从服务器获取该图像并返回到网页(我现在可以为一个图像执行此操作)。希望你能帮助。将多个字节数组映像合并为一个

这个在asp.net web表单中的代码创建一个图像,我将文件名作为webmethod函数的返回返回给浏览器。

Public Shared Function Base64ToImage(ByVal base64String As String, ByVal id As String) As String 
     'http://www.dailycoding.com/Posts/convert_image_to_base64_string_and_base64_string_to_image.aspx 
     ' Convert Base64 String to byte[] 

     Dim sFileName As String = String.Empty 

     Try 
      Dim imageBytes As Byte() = Convert.FromBase64String(base64String) 
      Dim ms As New MemoryStream(imageBytes, 0, imageBytes.Length) 

      ' Convert byte[] to Image 
      ms.Write(imageBytes, 0, imageBytes.Length) 
      Dim image__1 As Image = Image.FromStream(ms, True) 

      sFileName = "img_" & id & ".png" 

      Dim sPath As String = HttpContext.Current.Server.MapPath("images\") 

      image__1.Save(sPath & sFileName, System.Drawing.Imaging.ImageFormat.Png) 
     Catch ex As Exception 

     End Try 

     ' 
     Return sFileName 
    End Function 

我已经试过了,通过循环记录,然后试图将它们与sourcecopy结合,但我不能让它将它们结合起来?

Public Shared Function Base64ToImage2(ByVal dt As DataTable) As String 

     ' Convert Base64 String to byte[] 

     Dim sFileName As String = String.Empty 
     Dim base64String As String, id As String 

     'if first record create image 
     'on 2nd or greater in dt then combine images 
     Try 

      Dim iCount As Integer = 0 
      Dim image__1 As Image = Nothing 
      Dim compositeImage As Image = Nothing 
      Dim sPath As String = String.Empty 

      If dt.Rows.Count > 0 Then 
       For Each myRow As DataRow In dt.Rows 
        'getImage = getImage() & Base64ToImage(myRow("image_data").ToString(), myRow("id").ToString()) & "|" 

        If iCount = 0 Then 

         Dim imageBytes As Byte() = Convert.FromBase64String(myRow("image_data").ToString()) 
         Dim ms As New MemoryStream(imageBytes, 0, imageBytes.Length) 

         ' Convert byte[] to Image 
         ms.Write(imageBytes, 0, imageBytes.Length) 
         image__1 = System.Drawing.Image.FromStream(ms) 

         'sFileName = "img_1.png" 
         'sPath = HttpContext.Current.Server.MapPath("images\") 
         'image__1.Save(sPath & sFileName, System.Drawing.Imaging.ImageFormat.Png) 

         'compositeImage = New Bitmap(image__1.Width, image__1.Height) 

        Else 

         Dim imageBytes As Byte() = Convert.FromBase64String(myRow("image_data").ToString()) 
         Dim ms2 As New MemoryStream(imageBytes, 0, imageBytes.Length) 

         ' Convert byte[] to Image 
         ms2.Write(imageBytes, 0, imageBytes.Length) 
         Dim image__2 As Image = System.Drawing.Image.FromStream(ms2) 

         Dim g As Graphics = Graphics.FromImage(image__1) 
         g.CompositingMode = CompositingMode.SourceCopy 

         g.DrawImage(image__2, 0, image__1.Height) 

         sFileName = "img_1.png" 
         'sPath = HttpContext.Current.Server.MapPath("images\") 
         'image__2.Save(sPath & sFileName, System.Drawing.Imaging.ImageFormat.Png) 

        End If 
        iCount = iCount + 1 
       Next myRow 
      End If 

      'sFileName = "img_1.png" 
      'Dim sPath As String = HttpContext.Current.Server.MapPath("images\") 
      'compositeImage.Save(sPath & sFileName, System.Drawing.Imaging.ImageFormat.Png) 

     Catch ex As Exception 

     End Try 

     ' 
     Return sFileName 
    End Function 
+0

使用锂st adatapost 2012-01-06 05:21:44

+1

这是你在找什么http://stackoverflow.com/questions/465172/merging-two-images-in-c-net – 2012-01-06 05:46:07

+0

@Shoaib我有png的数据库中的字节64字符串,我转换成内存流(如上面Base64ToImage()中所示。我想将内存流中的“图像”合并为一个(彼此重叠)而不是使用物理图像。 – Rob 2012-01-06 14:50:02

回答

1

解决!经过大量的搜索和阅读,我能够将PNG图像合并为一个!每个图像都是从内存流创建的,然后用NewRectangle这个关键字附加到位图上。一旦我循环访问数据库中的记录,我就有一个图像在webmethod返回中下载到客户端。宽度和高度从客户端拉到webmethod并传递到函数中,以便缩放图像以适应浏览器的内部尺寸(以避免任何滚动条)。 mywidth = window.innerWidth myheight = window.innerHeight

转换的BASE64字节图像的代码如下...

Public Shared Function Base64ToImage2(ByVal dt As DataTable, ByVal Image_Width As String, ByVal Image_Height As String) As String 

     Dim sFileName As String = String.Empty 
     Dim sPath As String = HttpContext.Current.Server.MapPath("images\") 
     Dim myimage As Image = Nothing 

     ' Create a new bitmap object 400 pixels wide by 60 pixels high 
     Dim objBitmap As New Bitmap(CInt(Image_Width), CInt(Image_Height)) 

     '' Create a graphics object from the bitmap 
     Dim objGraphic As Graphics = Graphics.FromImage(objBitmap) 

     'if first record create image 
     'on 2nd or greater in dt then combine images 
     Try  

      If dt.Rows.Count > 0 Then 
       For Each myRow As DataRow In dt.Rows 


        Dim imageBytes As Byte() = Convert.FromBase64String(myRow("image_data").ToString()) 
        Dim ms As New MemoryStream(imageBytes, 0, imageBytes.Length) 

        ' Convert byte[] to Image 
        ms.Write(imageBytes, 0, imageBytes.Length) 
        myimage = System.Drawing.Image.FromStream(ms) 

        objGraphic.DrawImage(myimage, New Rectangle(0, 0, CInt(Image_Width), CInt(Image_Height))) 

       Next myRow 

       sFileName = "img_1.png" 
       objBitmap.Save(sPath & sFileName, System.Drawing.Imaging.ImageFormat.Png) 

      End If 



     Catch ex As Exception 

     End Try 

     ' 
     Return sFileName 
    End Function 
0

在:在客户端为尺寸上

JS情况下,其他人正在寻找在C#中,你正在试图用结果来加载图像源类似的东西,下面的代码:

 private void LoadImage() 
     { 
      string src = string.empty; 
      byte[] mergedImageData = new byte[0]; 

      mergedImageData = MergeTwoImageByteArrays(watermarkByteArray, backgroundImageByteArray); 
      src = "data:image/png;base64," + Convert.ToBase64String(mergedImageData); 
      MyImage.ImageUrl = src; 
     } 

     private byte[] MergeTwoImageByteArrays(byte[] imageBytes, byte[] imageBaseBytes) 
     { 
      byte[] mergedImageData = new byte[0]; 
      using (var msBase = new MemoryStream(imageBaseBytes)) 
      { 
       System.Drawing.Image imgBase = System.Drawing.Image.FromStream(msBase); 
       Graphics gBase = Graphics.FromImage(imgBase); 
       using (var msInfo = new MemoryStream(imageBytes)) 
       { 
        System.Drawing.Image imgInfo = System.Drawing.Image.FromStream(msInfo); 
        Graphics gInfo = Graphics.FromImage(imgInfo); 
        gBase.DrawImage(imgInfo, new Point(0, 0)); 
        //imgBase.Save(Server.MapPath("_____testImg.png"), ImageFormat.Png); 
        MemoryStream mergedImageStream = new MemoryStream(); 
        imgBase.Save(mergedImageStream, ImageFormat.Png); 
        mergedImageData = mergedImageStream.ToArray(); 
        mergedImageStream.Close(); 
       } 
      } 
      return mergedImageData; 
     }