Click here to Skip to main content
11,704,472 members (51,669 online)
Click here to Skip to main content

Save Bitmap as Targa-file

, 21 Feb 2013 CPOL 3.5K 2
Rate this:
Please Sign up or sign in to vote.
Save a 32-Bit RGBA-Bitmap in the Truevision Targa Format


In TV-broadcasting and game development, the Truevision Targa Format is very popular. The .NET Framework does not support this format. This class does save a given GDI+ bitmap to the 32-Bit version of the Targa-Format.


Information about the Truevision Targa format can be found here.

Using the Code

The class exports the shared sub SaveAsTarga. It has two arguments, Filename and Picture. Filename is the name under which the file is saved. Picture is a GDI+ Bitmap with PixelFormat.Format32bppArgb that shall be saved.

Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices

Public Class TargaFile

   Public Shared Sub SaveAsTarga(Filename As String, Picture As Bitmap)
      If Picture.PixelFormat <> Imaging.PixelFormat.Format32bppArgb Then
         Throw New Exception("Must be as 32-Bit Image")
         Exit Sub
      End If

      If Not IO.Directory.Exists(IO.Path.GetDirectoryName(Filename)) Then
         Throw New Exception("Path to save file does not exist")
         Exit Sub
      End If


      Using FS As New FileStream(Filename, FileMode.Create, FileAccess.Write)

         Dim bw As BinaryWriter = New BinaryWriter(FS)

         Dim sh As Int16 = 0

         bw.Write(CByte(0))   'IdentSize
         bw.Write(CByte(0))   'ColorMapType
         bw.Write(CByte(2))   'ImageType

         bw.Write(sh)         'ColorMapStart
         bw.Write(sh)         'ColorMapLength
         bw.Write(CByte(0))   'ColorMapBits

         bw.Write(sh)         'xStart
         bw.Write(sh)         'yStart

         sh = Picture.Width
         bw.Write(sh)         'Width
         sh = Picture.Height
         bw.Write(sh)         'Height

         bw.Write(CByte(32))  'Bits Per Pixel
         bw.Write(CByte(8))   'Descriptor

         Dim bmpData As BitmapData = Picture.LockBits(New Rectangle_
         	(0, 0, Picture.Width, Picture.Height), _
         	ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb)

         Dim ptr As IntPtr = bmpData.Scan0
         Dim bytes As Integer = bmpData.Stride * Picture.Height
         Dim rgbValues(bytes - 1) As Byte

         System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)


         Dim ln As Int32 = 0


         'Clean Up

      End Using

   End Sub

End Class 


  • First version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Switzerland Switzerland
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 21 Feb 2013
Article Copyright 2013 by DidiKunz
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid