Working with the RAW File Filter

The RAW File Filter can be used to load and save raw uncompressed data. The raw data can have:

Loading a file with the RAW filter

Parameters for loading raw uncompressed data are set through the LoadInformation event. When calling the file load functions, it is necessary to set the Format property of the RasterCodecs.Options.Load class to RasterImageFormat.Raw in order to trigger the LoadInformation event.

Inside the LoadInformation event, set the Format field of the CodecsLoadInformationEventArgs to RasterImageFormat.Raw. In addition, valid values must be set for the following properties: Width, Height, BitsPerPixel, and Offset (byte location in file where raw data begins). If each line of RAW data is padded so that the number of bytes is a multiple of 4 (as is the case with raw Windows BMP data), set the Pad4 property to true. Set the ViewPerspective property to load with the proper orientation. For example, raw Windows BMP data is stored with a BottomLeft orientation. If the orientation is unknown, set it as TopLeft. If the raw data is 8 bits per pixel or less, then the image is palettized and a palette must be generated. If this is the case, call SetPalette with a new RasterColor[] filling in the first (2^BitsPerPixel) entries. The Order property must be set with the appropriate RasterByteOrder enumeration such as Rgb, Bgr, or Gray.

Saving a file with the RAW filter

When saving raw uncompressed data, the raw data will correspond to the width, height, bits per pixel, color order, and view perspective of the image when it was loaded. For example, suppose a Window BMP file that is 300x400 24-bit color is loaded. If this file is saved as raw uncompressed data, the raw data is 24-bit, the color order is Bgr, with a view perspective BottomLeft (the first bytes in the file correspond to the bottom-left of the image), then the total size of the raw data file will be 300x400x3 = 360,000 bytes.

You can affect the way the raw data is saved by setting certain raw format specific parameters in the Save property of RasterCodecs.Options.Raw. The bits in each byte can be reversed by setting the ReverseBits property to true. Each line of raw data can be padded so that the length is a multiple of four bytes, by setting the Pad4 property to true. Other more generic save options that may be used can be set using the Save property of RasterCodecs.Options.

The raw data can be saved at any offset in the file by using a save overload such as Save(RasterImage,Stream,long,RasterImageFormat,int).

LEAD also supports saving Raw LZW data using the RasterImageFormat.RawLzw enumeration when saving the data. Note that when this format is used, the view perspective is ignored. The main purpose of this option is to provide support for saving LZW images that are embedded in other file formats such as PDF.

When using Save to save RAW data that is 8 bits per pixel or less, it is necessary to set the OptimizedPalette property of RasterCodecs.Options.Save to true to disable any dithering prior to the save. Note that no palette information is saved in the RAW file. Consequently, after loading RAW data, it is necessary to supply a palette (see Loading a file with the RAW filter above).


The following example demonstrates how to load and save raw uncompressed data.

// This example saves a RasterImage as RAW data starting at the offset given. 
// The data is padded so that each line of bytes is evenly divided by 4. 
// The bits in each byte are reversed before saving. 
// The bits per pixel of the raw data is the same as the bits per pixel of the RasterImage. 
// If image is a palettized image, the palette is not saved, only the raw data. 
// The OptimizedPalette is set to true to disable dithering on images with 8 bits or pixel or less. 
// Note that when saving a file as RAW data, the view perspective is ignored. 
private void SaveRawData(RasterImage image, string filename, int offset) 
	using (RasterCodecs codecs = new RasterCodecs()) 
		codecs.Options.Raw.Save.Pad4 = true; 
		codecs.Options.Raw.Save.ReverseBits = true; 
		codecs.Options.Save.OptimizedPalette = true; 
		if (offset == 0) 
			codecs.Save(image, filename, RasterImageFormat.Raw, 0); 
			using (FileStream stream = new FileStream(filename, FileMode.Create)) 
				codecs.Save(image, stream, offset, RasterImageFormat.Raw, 0); 
private struct RawData  
   public int Width;                               // Width of image  
   public int Height;                              // Height of image  
   public int BitsPerPixel;                        // Bits per pixel of image--if palettized, a gray palette is generated  
   public RasterViewPerspective ViewPerspective;   // View perspective of raw data (TopLeft, BottomLeft, etc)  
   public RasterByteOrder Order;                   // Rgb, Bgr, Gray etc 
   public int XResolution;                         // Horizontal resolution in DPI  
   public int YResolution;                         // Vertical resolution in DPI  
   public int Offset;                              // Offset into file where raw data begins  
   public bool Padding;                            // true if each line of data is padded to four bytes  
   public bool ReverseBits;                        // true if the bits of each byte are reversed   
private RawData myRawData; 
private void Codecs_LoadInformation(object sender, CodecsLoadInformationEventArgs e) 
	e.Format = RasterImageFormat.Raw; 
	e.Width = myRawData.Width; 
	e.Height = myRawData.Height; 
	e.BitsPerPixel = myRawData.BitsPerPixel; 
	e.XResolution = 150; 
	e.YResolution = 150; 
	e.Offset = myRawData.Offset; 
	e.Pad4 = myRawData.Padding; 
	e.Order = myRawData.Order; 
	e.LeastSignificantBitFirst = myRawData.ReverseBits; 
	e.ViewPerspective = myRawData.ViewPerspective; 
	// If image is palettized, create a grayscale palette 
	if (e.BitsPerPixel <= 8) 
		int colors = 1 << e.BitsPerPixel; 
		if (fixedPalette) 
			RasterColor[] palette = new RasterColor[colors]; 
			for (int i = 0; i < palette.Length; i++) 
				byte val = (byte)((i * 256) / colors); 
				palette[i] = new RasterColor(val, val, val); 
private void LoadRawData(string filename) 
	using (RasterCodecs codecs = new RasterCodecs()) 
		codecs.Options.Load.Format = RasterImageFormat.Raw; 
		codecs.LoadInformation += Codecs_LoadInformation; 
		return codecs.Load(filename); 

NOTE: For loading and saving from other sources such as a stream or URI, other overloads for Load and Save are provided.

Help Version 20.0.2020.4.3
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2020 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Imaging, Medical, and Document