Victor Image Processing Library How-to Tips

Convert a BMP Image to a JPEG

Converting from one image file format to another is easy with Victor. Determine the size of the image and allocate space for it in memory, load the BMP, and save the JPEG.

In general, to convert from one image format to another, the steps are:


C# Source Code (ASP.NET app) | C# Source Code (Desktop app) | VB Source Code | C/C++ Source Code | Java Source Code


BMP to JPEG - the C# Source Code for an ASP.NET Server Application

Requires Victor Image Processing Library for 32-bit Windows v 6 or higher.
<%@ Page Language="C#" %>
<%@ import namespace="System.Net" %>
<%@ import namespace="System.Runtime.InteropServices" %>
<%@ import namespace="viclib" %>

<head runat="server">
<title>Convert Image from BMP to JPEG File Format, bmp2jpeg</title>
<script runat="server">
[DllImport("kernel32.dll",EntryPoint="RtlMoveMemory")] public static extern void copybuffertobytearray(ref byte des, int src, int count);

void  Page_Load (object sender, EventArgs e){
    int rcode;
    int buffaddr=0;
    int buffsize;
    WebClient wc = new WebClient();
    vicwin.imgdes srcimg = new vicwin.imgdes();
    vicwin.BITMAPINFOHEADER binfo = new vicwin.BITMAPINFOHEADER();
    byte [] bmpimagedata = new byte[0];
    byte [] jpgimagedata = new byte[0];
    string imageurl = "http://www.catenary.com/test.bmp";

    bmpimagedata = wc.DownloadData(imageurl);     // Load a bmp image file into a byte array
    
    // Get the image dimensions
    rcode = vicwin.bmpinfofrombuffer(ref bmpimagedata[0], ref binfo);  
    if (rcode == vicwin.NO_ERROR) {   
        // Allocate space for the image in memory
        rcode = vicwin.allocimage(ref srcimg, binfo.biWidth, binfo.biHeight, binfo.biBitCount);
        if (rcode == vicwin.NO_ERROR) {    
        
            // Load the image into memory
            rcode = vicwin.loadbmpfrombuffer(ref bmpimagedata[0], ref srcimg);
            if (rcode == vicwin.NO_ERROR) {  
            
                if (binfo.biBitCount == 1) {   // If this is a 1-bit image, convert to 8-bit
                    vicwin.imgdes timage = new vicwin.imgdes();    // For compatibility with jpeg format

                    rcode = vicwin.allocimage(ref timage, binfo.biWidth, binfo.biHeight, 8);
                    rcode = vicwin.convert1bitto8bit(ref srcimg, ref timage);
                    if (rcode == vicwin.NO_ERROR) {  // Success! Replace the 1-bit image with the 8-bit image
                        vicwin.freeimage(ref srcimg);
                        vicwin.copyimgdes(ref timage, ref srcimg);
                        }
                    }
            
                // Convert the bmp into jpeg file format (quality = 50)
                rcode = vicwin.savejpgtobuffer(ref buffaddr, ref srcimg, 50);
                buffsize = vicwin.getbuffersize(buffaddr);
                jpgimagedata = new byte[buffsize];  
                
                // Put the jpeg file into a byte array
                copybuffertobytearray(ref jpgimagedata[0], buffaddr, buffsize);
                vicwin.freebuffer(buffaddr);
            }
            vicwin.freeimage(srcimg);  // Release the image memory
        }
    }        
    
   Response.ContentType = "image/jpeg";
   Response.Expires = 0;
   Response.Buffer = true;
   Response.Clear();
   Response.BinaryWrite(jpgimagedata);  // Send the image to the browser
   Response.End();
}











BMP to JPEG - the C# Source Code for a Desktop Application

Requires Victor Image Processing Library for 32-bit Windows v 6 or higher.

using viclib;
 
 . . . 
 
int bmp2jpeg( string src_fname,  string des_fname)
{
   vicwin.imgdes tmpimage = new vicwin.imgdes();
   int rcode, quality=75, vbitcount;
   vicwin.BITMAPINFOHEADER bdat = new vicwin.BITMAPINFOHEADER(); // Reserve space for BMP struct

   // Get info on the file we're to load 
   rcode = vicwin.bmpinfo(ref src_fname, ref bdat);
   if(rcode != vicwin.NO_ERROR) {
      return(rcode);
      }

   vbitcount = bdat.biBitCount;
   if(vbitcount >= 16)   // 16-, 24-, or 32-bit image is loaded into 24-bit buffer
      vbitcount = 24;

   // Allocate space for an image
   rcode = vicwin.allocimage(ref tmpimage, bdat.biWidth, bdat.biHeight, vbitcount);
   if(rcode != vicwin.NO_ERROR) {
      return(rcode);
      }

   // Load image 
   rcode = vicwin.loadbmp(ref src_fname, ref tmpimage);
   if(rcode != vicwin.NO_ERROR) {
       vicwin.freeimage(ref tmpimage); // Free image on error
      return(rcode);
      }


   if(vbitcount == 1) {  // If we loaded a 1-bit image, convert to 8-bit grayscale
       // because jpeg only supports 8-bit grayscale or 24-bit color images
       vicwin.imgdes tmp2image = new vicwin.imgdes();

       rcode = vicwin.allocimage(ref tmp2image, bdat.biWidth, bdat.biHeight, 8);
       if (rcode == vicwin.NO_ERROR)
       {
           vicwin.convert1bitto8bit(ref tmpimage, ref tmp2image);
           vicwin.freeimage(ref tmpimage);  // Replace 1-bit image with grayscale image
           vicwin.copyimgdes(ref tmp2image, ref tmpimage);
         }
      }

   // Save image
      rcode = vicwin.savejpg(ref des_fname, ref tmpimage, quality);

      vicwin.freeimage(ref tmpimage);
   return(rcode);
}



BMP to JPEG - the Visual Basic Source Code

Requires Victor Image Processing Library for 32-bit Windows v 5 or higher.

Private Sub mnuconvertBMPtoJPG_Click()
   Dim tmpimage As imgdes    ' Image descriptors
   Dim tmp2image As imgdes
   Dim rcode As Long
   Dim quality As Long
   Dim vbitcount As Long
   Dim bdat As BITMAPINFOHEADER ' Reserve space for BMP struct
   Dim bmp_fname As String
   Dim jpg_fname As String

   bmp_fname = "test.bmp"
   jpg_fname = "test.jpg"

   quality = 75
   ' Get info on the file we're to load
   rcode = bmpinfo(bmp_fname, bdat)
   If (rcode <> NO_ERROR) Then
      MsgBox "Cannot find file", 0, "Error encountered!"
      Exit Sub
   End If
    
   vbitcount = bdat.biBitCount
   If (vbitcount >= 16) Then  ' 16-, 24-, or 32-bit image is loaded into 24-bit buffer
      vbitcount = 24
   End If
   
   ' Allocate space for an image
   rcode = allocimage(tmpimage, bdat.biWidth, bdat.biHeight, vbitcount)
   If (rcode <> NO_ERROR) Then
     MsgBox "Not enough memory", 0, "Error encountered!"
     Exit Sub
   End If
   
   ' Load image
   rcode = loadbmp(bmp_fname, tmpimage)
   If (rcode <> NO_ERROR) Then
      freeimage tmpimage ' Free image on error
      MsgBox "Cannot load file", 0, "Error encountered!"
      Exit Sub
   End If

   If (vbitcount = 1) Then ' If we loaded a 1-bit image, convert to 8-bit grayscale
       ' because jpeg only supports 8-bit grayscale or 24-bit color images
     rcode = allocimage(tmp2image, bdat.biWidth, bdat.biHeight, 8)
     If (rcode = NO_ERROR) Then
         rcode = convert1bitto8bit(tmpimage, tmp2image)
         freeimage tmpimage  ' Replace 1-bit image with grayscale image
         copyimgdes tmp2image, tmpimage
     End If
   End If

   ' Save image
   rcode = savejpg(jpg_fname, tmpimage, quality)
   freeimage tmpimage
 
End Sub


BMP to JPEG - the C Source Code

Requires Victor Image Processing Library v 4.25 or higher.
int bmp2jpeg(char far *src_fname, char far *des_fname)
{
   imgdes tmpimage;
   int rcode, quality=75, vbitcount;
   BITMAPINFOHEADER bdat; // Reserve space for BMP struct

   // Get info on the file we're to load 
   rcode = bmpinfo(src_fname, &bdat);
   if(rcode != NO_ERROR) {
      return(rcode);
      }

   vbitcount = bdat.biBitCount;
   if(vbitcount >= 16)   // 16-, 24-, or 32-bit image is loaded into 24-bit buffer
      vbitcount = 24;

   // Allocate space for an image
   rcode = allocimage(&tmpimage, (int)bdat.biWidth, (int)bdat.biHeight, vbitcount);
   if(rcode != NO_ERROR) {
      return(rcode);
      }

   // Load image 
   rcode = loadbmp(src_fname, &tmpimage);
   if(rcode != NO_ERROR) { 
      freeimage(&tmpimage); // Free image on error
      return(rcode);
      }


   if(vbitcount == 1) {  // If we loaded a 1-bit image, convert to 8-bit grayscale
       // because jpeg only supports 8-bit grayscale or 24-bit color images
      imgdes tmp2image;

      rcode = allocimage(&tmp2image, (int)bdat.biWidth, (int)bdat.biHeight, 8);
      if(rcode == NO_ERROR) {
         convert1bitto8bit(&tmpimage, &tmp2image);
         freeimage(&tmpimage);  // Replace 1-bit image with grayscale image
         copyimgdes(&tmp2image, &tmpimage);
         }
      }

   // Save image
   rcode = savejpg(des_fname, &tmpimage, quality);

   freeimage(&tmpimage);
   return(rcode);
}



BMP to JPEG - the Java Source Code

Requires Victor Image Processing Library v 5.3 or higher, the Victor Java Native Interface vic.zip and Java 2.

Place the dlls:

vic32.dll in x:\java\bin
victw32.dll in x:\java\bin
vic32jni.dll in the same subdirectory as the application

To compile and run bmp2jpeg.class:

javac -classpath x:\java bmp2jpeg.java
java -classpath x:\java;x:\java\vic bmp2jpeg

 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


import vic.*;               // The Victor Java Native Interface

/*

   javac -classpath e:\java bmp2jpeg.java
   java -classpath e:\java;e:\java\vic bmp2jpeg

*/

class bmp2jpeg {

   public static void main(String args[]) {

   String loadfilename;
   String savefilename;
   int rcode = 0; 
   vic.imgdes timage = new vic.imgdes();
   vic.BITMAPINFOHEADER filedata = new vic.BITMAPINFOHEADER();

   rcode = vic.vic32jni.enableunicode(1);  // Turn on Unicode if available

   // load bmp save as jpeg
   loadfilename = "test.bmp";
   savefilename = "test.jpg";
   rcode = vic.vic32jni.bmpinfo(loadfilename, filedata);
   if(rcode == vic.vic32jni.NO_ERROR) {
      rcode = vic.vic32jni.allocimage(timage, filedata.biWidth, filedata.biHeight, (int)filedata.biBitCount);

      if(rcode == vic.vic32jni.NO_ERROR) {
         System.out.println("Allocation successful, loading bmp file");
         System.out.println("  image width in pixels = " + filedata.biWidth);
         System.out.println("  image length in pixels = " + filedata.biHeight);
         System.out.println("  buffer width in bytes = " + timage.buffwidth);
         rcode = vic32jni.loadbmp(loadfilename, timage);
         if(rcode == vic.vic32jni.NO_ERROR) {
            System.out.println("File load successful, saving jpeg file");
            if(timage.bibitcount == 1) 
               rcode = convert2grayscale(timage);

            rcode = vic.vic32jni.savejpg(savefilename, timage, 75);
            System.out.println("Filesave rcode = " + rcode);
            }
         }
      if(rcode == 0)
         vic.vic32jni.freeimage(timage);
      }
   } // end of main()


   public static void displayimgdes(imgdes rimage) {

   System.out.println("     Image buffer address        " + rimage.ibuff);        
   System.out.println("     Image area to be processed  " + rimage.stx +", "+ rimage.sty +", "+ rimage.endx +", "+ rimage.endy); 
   System.out.println("     Image buffer width in bytes " + rimage.buffwidth);    
   System.out.println("     Address of palette          " + rimage.palette);      
   System.out.println("     Palette colors              " + rimage.colors);       
   System.out.println("     Image type                  " + rimage.imgtype);      
   System.out.println("     BITMAPINFOHEADER address    " + rimage.bmh);          
   System.out.println("     DIB handle                  " + rimage.hBitmap);      
   System.out.println("     Pixel width of image        " + rimage.biwidth);      
   System.out.println("     Pixel length of image       " + rimage.biheight);     
   System.out.println("     Pixel depth of image        " + rimage.bibitcount);   

   }


   public static int convert2grayscale(imgdes rimage) {
   vic.imgdes grayimage = new vic.imgdes();
   int rcode;

   System.out.println("Converting from 1- to 8-bit");
   rcode = vic.vic32jni.allocimage(grayimage, rimage.biwidth, rimage.biheight, 8);
   if(rcode == vic.vic32jni.NO_ERROR) {
      rcode = vic.vic32jni.convert1bitto8bit(rimage, grayimage);
      if(rcode == vic.vic32jni.NO_ERROR) {
         System.out.println("Conversion successful");
         System.out.println("1-bit image:");
         displayimgdes(rimage);
         System.out.println("8-bit image:");
         displayimgdes(grayimage);
         vic.vic32jni.freeimage(rimage);
         vic.vic32jni.copyimgdes(grayimage, rimage);
         System.out.println("Replacing original image with:");
         displayimgdes(rimage);
         }
      }
   return(rcode); // 8-bit image replaces the original 1-bit image
   }

}

Victor Image Processing Library

Victor Image Processing Library homepage | Victor Product Summary | more source code




Copyright © 1995 - 2001 Catenary Systems Inc. All rights reserved. Victor Image Processing Library is a trademark of Catenary Systems.