SQL Server, T-SQL, ASP.NET, Javascript, SAP, ABAP Programming

Kodyaz Development Resources

Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Vista, etc.
Welcome to Kodyaz Development Resources Sign in | Join | Help




.NET Framework, ASP.NET

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

ASP.NET Forums

Visual Studio Forums

Windows Forms Forums

Web Services Forums

LINQ Forums

VSTS and TFS Forums

.NET Development Blog

ASP.NET Ajax Blog

TechEd Developers 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)






Free Exam Vouchers











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