SQL Server administration and T-SQL development, Web Programming with ASP.NET and Javascript, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software Kodyaz Development Resources
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Vista, etc.






.NET Framework, ASP.NET

ASP.NET, Windows Forms, Controls, .NET Framework and Visual Studio Articles

ASP.NET Forums

Visual Studio Forums

.NET Development Blog

Certification Exams Blog




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)











Copyright © 2004 - 2011 Eralper Yilmaz. All rights reserved.
Community Server, by Telligent Systems