将多个字节数组图像为一体 [英] Combine multiple byte array images into one

查看:121
本文介绍了将多个字节数组图像为一体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在asp.net web表单这code创建我返回文件名作为一个WebMethod函数返回给浏览器返回的图像。

 公共共享功能Base64ToImage(BYVAL base64String作为字符串,BYVAL ID作为字符串)作为字符串
        http://www.dailycoding.com/Posts/convert_image_to_base64_string_and_base64_string_to_image.aspx
        base64转换的字符串为byte []        昏暗sFileName作为字符串=的String.Empty        尝试
            昏暗imageBytes为字节()= Convert.FromBase64String(base64String)
            昏暗的MS作为新的MemoryStream(imageBytes,0,imageBytes.Length)            转换的byte []到图像
            ms.Write(imageBytes,0,imageBytes.Length)
            昏暗image__1作为图像= Image.FromStream(MS,真)            sFileName =IMG_与& ID&安培; 巴纽            昏暗SPATH作为字符串= HttpContext.Current.Server.MapPath(影像\\)            image__1.Save(SPATH&安培; sFileName,System.Drawing.Imaging.ImageFormat.Png)
        抓住EX为例外        结束Try        
        返回sFileName
    结束功能

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

 公共共享功能Base64ToImage2(DT BYVAL作为DataTable中)作为字符串        base64转换的字符串为byte []        昏暗sFileName作为字符串=的String.Empty
        昏暗base64String作为字符串,ID作为字符串        如果第一个记录创建图像
        在DT第二或大于合并图像
        尝试            昏暗ICOUNT为整数= 0
            昏暗image__1作为图像=什么
            昏暗compositeImage作为图像=什么
            昏暗SPATH作为字符串=的String.Empty            如果dt.Rows.Count> 0,则
                对于每个myRow作为的DataRow在dt.Rows
                    '的getImage =的getImage()及Base64ToImage(myRow(IMAGE_DATA)的ToString(),myRow(ID)的ToString()。)及|                    如果ICOUNT = 0。然后                        昏暗imageBytes为字节()= Convert.FromBase64String(myRow(IMAGE_DATA)。toString()方法)
                        昏暗的MS作为新的MemoryStream(imageBytes,0,imageBytes.Length)                        转换的byte []到图像
                        ms.Write(imageBytes,0,imageBytes.Length)
                        image__1 = System.Drawing.Image.FromStream(毫秒)                        'sFileName =img_1.png
                        SPATH = HttpContext.Current.Server.MapPath(影像\\)
                        image__1.Save(SPATH&安培; sFileName,System.Drawing.Imaging.ImageFormat.Png)                        compositeImage =新位图(image__1.Width,image__1.Height)                    其他                        昏暗imageBytes为字节()= Convert.FromBase64String(myRow(IMAGE_DATA)。toString()方法)
                        昏暗MS2作为新的MemoryStream(imageBytes,0,imageBytes.Length)                        转换的byte []到图像
                        ms2.Write(imageBytes,0,imageBytes.Length)
                        昏暗image__2作为图像= System.Drawing.Image.FromStream(MS2)                        昏暗摹作为图形= 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(影像\\)
                        image__2.Save(SPATH&安培; sFileName,System.Drawing.Imaging.ImageFormat.Png)                    万一
                    ICOUNT = ICOUNT + 1
                接下来myRow
            万一            'sFileName =img_1.png
            昏暗SPATH作为字符串= HttpContext.Current.Server.MapPath(影像\\)
            compositeImage.Save(SPATH&安培; sFileName,System.Drawing.Imaging.ImageFormat.Png)        抓住EX为例外        结束Try        
        返回sFileName
    结束功能


解决方案

解决了!一吨搜索和阅读后,我能够png图片合并成一个!每个图像从存储器流创建,然后附加到与NewRectangle位图这是关键。一旦通过从数据库中的记录我环,我有被下载到客户端的一个的webmethod返回一个图像。宽度和高度都从客户端将WebMethod拉和传递给函数,因此对图像进行缩放以适应浏览器的内部尺寸(以避免任何滚动条)。

JS客户端的尺寸上:
 mywidth = window.innerWidth
 myheight = window.innerHeight

在code到的base64字节的图像转换为如下...

 公共共享功能Base64ToImage2(DT BYVAL作为DataTable中,BYVAL IMAGE_WIDTH作为字符串,BYVAL IMAGE_HEIGHT作为字符串)作为字符串        昏暗sFileName作为字符串=的String.Empty
        昏暗SPATH作为字符串= HttpContext.Current.Server.MapPath(影像\\)
        昏暗MYIMAGE作为图像=什么        60像素高创建一个新的位图对象宽400像素
        昏暗objBitmap作为新位图(CINT(IMAGE_WIDTH),CINT(IMAGE_HEIGHT))        ''创建一个图形从位图对象
        昏暗objGraphic作为图形= Graphics.FromImage(objBitmap)        如果第一个记录创建图像
        在DT第二或大于合并图像
        尝试            如果dt.Rows.Count> 0,则
                对于每个myRow作为的DataRow在dt.Rows
                    昏暗imageBytes为字节()= Convert.FromBase64String(myRow(IMAGE_DATA)。toString()方法)
                    昏暗的MS作为新的MemoryStream(imageBytes,0,imageBytes.Length)                    转换的byte []到图像
                    ms.Write(imageBytes,0,imageBytes.Length)
                    MYIMAGE = System.Drawing.Image.FromStream(毫秒)                    objGraphic.DrawImage(MYIMAGE,新的Rectangle(0,0,CINT(IMAGE_WIDTH),CINT(IMAGE_HEIGHT)))                接下来myRow                sFileName =img_1.png
                objBitmap.Save(SPATH&安培; sFileName,System.Drawing.Imaging.ImageFormat.Png)            万一        抓住EX为例外        结束Try        
        返回sFileName
    结束功能

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

解决方案

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

这篇关于将多个字节数组图像为一体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆