Monday, October 1, 2007

How to convert image between different format?

/// <summary>
/// Convert image file to certain format
/// </summary>
/// <param name="fromImageFullPath">The original image file</param>
/// <param name="toImageFullPath">The new image file</param>
/// <param name="mimeType">The format of the new image file, i.e. "image/bmp", "image/jpeg"</param>
public static void ConvertImage(string fromImageFullPath, string toImageFullPath, string mimeType)
{
try
{
// Read the content of the image file into a bitmap variable
Stream fromFile = File.OpenRead(fromImageFullPath);
Image image = Image.FromStream( fromFile, true ) ;
Bitmap bitmap = (Bitmap) image;
// Instantiate the encoder
EncoderParameters encoderParams = new EncoderParameters();
encoderParams.Param[0] = new EncoderParameter( Encoder.Quality, 50L );
ImageCodecInfo codecInfo = GetEncoderInfo( mimeType );
// Covert the image to the new format
MemoryStream newImage = new MemoryStream();
bitmap.Save( newImage, codecInfo, encoderParams );
// Write the new image into a file
byte[] data = newImage.ToArray();
Stream toFile = File.OpenWrite( toImageFullPath );
toFile.Write(data,0,data.GetLength(0));
fromFile.Close();
toFile.Close();
}
catch(Exception ex)
{
...
}
}



/// <summary>
/// Get encoder infor
/// </summary>
/// <param name="mimeType">the codec's Multipurpose Internet Mail Extensions (MIME) type</param>
/// <returns>All pertinent information about the installed image codecs</returns>
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for(j = 0; j < encoders.Length; ++j)
{
if(encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}

How to print a TIF file?

The following code is in C#.

// Print TIF file
PrintDocument printDocument = new PrintDocument();
printDocument.PrinterSettings.PrinterName = printerIdentifier;
printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
printDocument.Print();

/// <summary>
/// This event occurs when the output to print for the current page is needed
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">an argument of type PrintPageEventArgs containing data related to this event</param>
private static void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
try
{
Graphics g = e.Graphics;
Stream fs = File.OpenRead(_fileName);
Image image = Image.FromStream( fs, true ) ;
g.DrawImage(image,0,0);
fs.Close();
}
catch(Exception ex)
{
...
}
}

How to read the content of a file into Byte and write Byte to a file?

public static byte[] ReadFileToByte(string fileName)
{
byte[] data = null;

// Check whether the image file exists or not
if (fileName == null || fileName.Length <= 0 || !File.Exists(fileName))
{
return data;
}

try
{
// Open the image file
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
// Read the image into the byte variable
data = br.ReadBytes((int)fs.Length);

br.Close();
fs.Close();
}
catch(Exception ex)
{
...
}

return data;
}

/// <summary>
/// Write the content of a byte variable into a file
/// </summary>
/// <param name="data">The byte variable</param>
/// <param name="fileName">The name of the target file</param>
public static void WriteByteToFile(byte[] data, string fileName)
{
// Check whether there is data in the byte variable
if ((data==null) || (data.Length<=0))
{
return;
}

if (fileName==null||fileName.Trim()==String.Empty)
{
return;
}
try
{
// Open the target file. If it doesn't exist, create it.
FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
// Write the content of the byte variable into the file
bw.Write(data);

bw.Flush();
bw.Close();
fs.Close();

}
catch(Exception ex)
{
...
}
}

How to print PDF using GhostScript?

// Print PDF file using GhostScript
System.Diagnostics.ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Arguments = " -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=" + Convert.ToString(numberOfCopies) + " -sDEVICE=ljet4 -sOutputFile=\"\\\\spool\\" +
printerIdentifier + "
\" \"" + _fileName + "\"";
startInfo.FileName = ConfigurationSettings.AppSettings["GhostScriptPath"].ToString();
startInfo.UseShellExecute = false;
System.Diagnostics.Process process = Process.Start(startInfo);