In general, to convert from one image format to another, the steps are:
<%@ 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();
}