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

发布于 2024-12-25 08:19:41 字数 4031 浏览 0 评论 0原文

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

I can create an image on the server from a byte array stored in the database. But how do I combine each byte array into one image. Basically I want to stack them on top of each other (they are all 1366px width and 618px height) and then save that to a png image. I will then get that image from the server and return to the web page (which I can do now for one image). Hope you can help.

This code in asp.net web forms creates an image which i return the filename as a return in a webmethod function back to the browser.

 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

I have tried this, looping through the records and then trying to combine them with sourcecopy but I can't get it to combine them?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小女人ら 2025-01-01 08:19:41

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

客户端上的 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

Solved! After a ton of searching and reading I was able to combine png images into one! Each image is created from a memory stream and then appended to a bitmap with NewRectangle which is the key. Once I loop through the records from the database, I have one image which is downloaded to the client in a webmethod return. The width and height are pulled from the client to the webmethod and passed into the function so the image is scaled to fit the browser inner dimensions (to avoid any scrollbars).

JS on the client for the dimensions:
mywidth = window.innerWidth
myheight = window.innerHeight

The code to convert the base64 byte image is as follows...

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
抽个烟儿 2025-01-01 08:19:41

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

In case someone else is looking for something similar in C# where you're trying to load an image source with the result, here's the code:

        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;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文