SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
ASP.NET, VB.NET, Microsoft .NET Framework, Microsoft Visual Studio, Windows Forms, Controls and more Tutorials and Articles for Programmers


How to Convert Bitmap to Byte Array in order to Display Image from Resource File


I need to convert a bitmap to byte array in order to display an image from resource file on a web page in a web application.
I read the image file from App_GlobalResources resources by explicitly calling the resource file.
But I had to convert to a byte array in order to send the bitmap file as a response.

The trick is using the MemoryStream object from the System.IO.MemoryStream namaspace.

'Read bitmap resource
Dim bitmap As Bitmap = Resources.GlobalSiteResources.logo

'Convert to Byte array
Dim ConvertedByteArray As Byte()

Using memorystream As New System.IO.MemoryStream
   bitmap.Save(memorystream, bitmap.RawFormat)
   ConvertedByteArray = memorystream.ToArray
End Using





And here is the all code I used to display an image file from web applications resources that are stored in a Global Resource file in App_GlobalResources ASP.NET folder.

'SET Current UI Culture
Dim currentCultureInfo As CultureInfo = CultureInfo.GetCultureInfo("tr-TR")
System.Threading.Thread.CurrentThread.CurrentUICulture = currentCultureInfo


'Read bitmap resource
Dim bitmap As Bitmap = Resources.GlobalSiteResources.logo

'Convert to Byte array
Dim ConvertedByteArray As Byte()

Using memorystream As New System.IO.MemoryStream
   bitmap.Save(memorystream, bitmap.RawFormat)
   ConvertedByteArray = memorystream.ToArray
End Using

'Response
Response.Buffer = True
Response.ContentType = "Image/BMP"

Response.BinaryWrite(ConvertedByteArray)



Visual Studio


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.