JDBC : Executing stored procedures 02 November 2004 at 00:00

To execute a stored procedure on your database server with JDBC, you need to prepare a CallableStatement using your active connection. Input and output parameters but be explicitly declared.

For example, a stored procedure with an output parameter called "@pRet" and two input parameters "@p1" and "@p2":

  CallableStatement cstmt = null;
  double res = 0.0;
  try {
    cstmt = conn.prepareCall("{ ? = call MyProc(?,?) }");
    cstmt.registerOutParameter(1, Types.NUMERIC);
    cstmt.setDouble(2, 1.0);
    cstmt.setDouble(3, 2.0);
    cstmt.execute();
    res = cstmt.getDouble(1);
  }
  catch (SQLException e) {
    ...
  }
  finally {
    try {
      cstmt.close();
    }
    catch (Exception e) { }
  }
Downloading images via HTTP with C# 03 November 2004 at 00:00
// do the network request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// add proxy information here if applicable
NetworkCredential cred = new NetworkCredential("ProxyUser", "ProxyPassword", "PROXYDOMAIN");
request.Proxy = new WebProxy("192.168.0.1:8080");
request.Proxy.Credentials = cred;
// add remote authentication information here if applicable
request.Credentials = new NetworkCredential("RemoteUser", "RemotePassword");
// get the response - raise an exception if not OK
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK) {
  throw new Exception("Bad response received from web server while downloading image: " + response.StatusCode.ToString());
}
// response received - check size of the response 
long length = response.ContentLength;
if (length <= 0) {
  throw new Exception("No content received from url "" + url + """);
}
return Image.FromStream(response.GetResponseStream());
C#: Resizing images 22 November 2004 at 00:00
The following function can be used to resize images on your machine. Note that this function works out the image format internally and creates a file of the same format. You may want to adjust this to output a file of a different format.

  /// <summary>
  /// This function can be used to resize images, while retaining image quality 
  /// that cannot be done when using the GetThumbnailImage method.
  /// </summary>
  /// <param name="sourceImgPath">Path to the source image</param>
  /// <param name="outputImgPath">Path to the output image</param>
  /// <param name="width">Width of the output image</param>
  /// <param name="height">height of the output image</param>
  /// <param name="quality">Quality (valid values 0 - 100)</param>
  private void ResizeImage(string sourceImgPath, string outputImgPath, int width, int height, long quality) 
  {
    ImageCodecInfo codecInfo = null;
    Bitmap bIn = new Bitmap(sourceImgPath, true);
    // get the correct encoder information
    Guid formatGuid = bIn.RawFormat.Guid;
    ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
    for(int i=0; i<encoders.Length; i++)
    {
      if (encoders[i].FormatID == formatGuid) 
      {
        codecInfo = encoders[i];
      }
    }    if (codecInfo == null) throw new Exception("Unable to find image encoder for file " + sourceImgPath);
    // now resize the image
    EncoderParameters encoderParams = new EncoderParameters(1);
    encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, quality);
    Bitmap bOut = ScaleImage(bIn, width, height, quality);
    bOut.Save(outputImgPath, codecInfo, encoderParams);
    bIn.Dispose();
    bOut.Dispose();
  }

The following scaling function is also required:

  private Bitmap ScaleImage(Bitmap bitmap, int width, int height, long quality) 
  {
    Bitmap bOut = new Bitmap(width, height);
    Graphics g = Graphics.FromImage(bOut);
    g.SmoothingMode = SmoothingMode.HighQuality;
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.DrawImage(bitmap, 0, 0, width, height); 
    g.Dispose();
    return bOut;
  }