Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Save Bitmap as Targa-file

0.00/5 (No votes)
21 Feb 2013 1  
Save a 32-Bit RGBA-Bitmap in the Truevision Targa Format

Introduction

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.

Background

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

      Picture.RotateFlip(RotateFlipType.RotateNoneFlipY)

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

         Dim bw As BinaryWriter = New BinaryWriter(FS)

         'Header
         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

         'Bitmap
         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)
         bw.Write(rgbValues)

         Picture.UnlockBits(bmpData)

         'Footer
         Dim ln As Int32 = 0
         bw.Write(ln)
         bw.Write(ln)

         bw.Write("TRUEVISION-XFILE.")
         bw.Write(CByte(0))

         'Clean Up
         bw.Flush()
         FS.Flush()
         bw.Close()
         FS.Close()

      End Using

   End Sub

End Class 

History

  • First version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here