将多字节数组图像合并为一个

时间:2012-01-06 05:18:43

标签: asp.net image byte memorystream

我可以从存储在数据库中的字节数组在服务器上创建一个图像。但是如何将每个字节数组合成一个图像。基本上我想将它们堆叠在一起(它们都是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

2 个答案:

答案 0 :(得分:1)

解决!经过大量的搜索和阅读后,我能够将png图像合二为一!每个图像都是从内存流中创建的,然后使用NewRectangle作为关键字附加到位图。一旦我从数据库循环记录,我有一个图像,它在webmethod返回下载到客户端。宽度和高度从客户端拉到web方法并传递到函数中,因此缩放图像以适应浏览器内部尺寸(以避免任何滚动条)。

客户端上的JS维度:  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

答案 1 :(得分:0)

如果其他人在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;
        }