SQL Server Image Resize and Formate/ Type Conversion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;
using System.Configuration;

namespace ug_admission
{
public partial class ResizeConvertImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
try
{
if (Session[“_AdminLogin”].ToString() == “OK”)
{
}
else
{
Response.Redirect(“frmAdminLogin.aspx”);
}
}
catch (Exception ex) { }
}
}

public static byte[] ImageToByte(System.Drawing.Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}

protected void Button1_Click(object sender, EventArgs e)
{
string strConn = ConfigurationManager.ConnectionStrings[“ug_admission”].ConnectionString;
//”uid=MyUserId;pwd=MyPassword;server=MyPictureServer;database=MyDatabaseName;Connect Timeout=600″;
SqlConnection conn = null;
SqlCommand cmd = null;
//Int32 counter = 0;
byte[] imagedata = null;
byte[] signaturedata = null;
Int32 intDesiredWidth = 250;
try
{
conn = new SqlConnection(strConn);
cmd = new SqlCommand();
cmd.Connection = conn;
//cmd.CommandText = “SELECT PHOTO, APPLICATION_SN, SIGNATURE FROM ug_applicant_general_info_PAID”;
cmd.CommandText = “SELECT PHOTO, APPLICATION_SN, SIGNATURE FROM ug_applicant_general_info_PAID”;
//cmd.CommandTimeout = 60;
cmd.CommandType = CommandType.Text;
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
String Sl = reader[1].ToString();
imagedata = (byte[])reader[0];
System.Drawing.Image tmpOriginalImage = System.Drawing.Image.FromStream(new System.IO.MemoryStream(imagedata));
double dblScaleImg = (double)intDesiredWidth / (double)tmpOriginalImage.Width;
Graphics tmpGraphics = default(Graphics);
Bitmap tmpResizedImage = new Bitmap(Convert.ToInt32(dblScaleImg * tmpOriginalImage.Width), Convert.ToInt32(dblScaleImg * tmpOriginalImage.Height));
tmpGraphics = Graphics.FromImage(tmpResizedImage);
tmpGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
tmpGraphics.DrawImage(tmpOriginalImage, 0, 0, tmpResizedImage.Width + 1, tmpResizedImage.Height + 1);
System.Drawing.Image imageOut = tmpResizedImage;
//System.Drawing.Image imageOut = tmpOriginalImage;
imageOut.Save(“D:\\PicturesFolder\\SN_” + Sl + “.jpg”, System.Drawing.Imaging.ImageFormat.Jpeg); //counter++;

signaturedata = (byte[])reader[2];
System.Drawing.Image tmpOriginalSig = System.Drawing.Image.FromStream(new System.IO.MemoryStream(signaturedata));
double dblScaleSig = (double)intDesiredWidth / (double)tmpOriginalSig.Width;
Graphics tmpGraphicsSig = default(Graphics);
Bitmap tmpResizedSig = new Bitmap(Convert.ToInt32(dblScaleSig * tmpOriginalSig.Width), Convert.ToInt32(dblScaleSig * tmpOriginalSig.Height));
tmpGraphicsSig = Graphics.FromImage(tmpResizedSig);
tmpGraphicsSig.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
tmpGraphicsSig.DrawImage(tmpOriginalSig, 0, 0, tmpResizedSig.Width + 1, tmpResizedSig.Height + 1);
System.Drawing.Image sigOut = tmpResizedSig;
//System.Drawing.Image imageOut = tmpOriginalImage;
sigOut.Save(“D:\\SignatureFolder\\SN_” + Sl + “_SiG.jpg”, System.Drawing.Imaging.ImageFormat.Jpeg); //counter++;

//byte[] resizedImage = ImageToByte(tmpResizedImage);

//try
//{
// String qs = ” UPDATE ug_applicant_general_info_PAID SET PHOTO=@PHOTO WHERE APPLICATION_SN=@APPLICATION_SN “;
// SqlCommand cmd2 = new SqlCommand(qs, conn);
// cmd2.CommandType = CommandType.Text;
// cmd2.Parameters.Add(new SqlParameter(“@PHOTO”, resizedImage));
// cmd2.Parameters.Add(new SqlParameter(“@APPLICATION_SN”, Sl.Trim()));
// int result = cmd2.ExecuteNonQuery();
//}
//catch (Exception ex) { }

//try
//{
// String qs = ” UPDATE ug_applicant_general_info_PAID SET SIGNATURE=@SIGNATURE WHERE APPLICATION_SN=@APPLICATION_SN “;
// SqlCommand cmd2 = new SqlCommand(qs, conn);
// cmd2.CommandType = CommandType.Text;
// cmd2.Parameters.Add(new SqlParameter(“@SIGNATURE”, resizedImage));
// cmd2.Parameters.Add(new SqlParameter(“@APPLICATION_SN”, Sl.Trim()));
// int result = cmd2.ExecuteNonQuery();
//}
//catch (Exception ex) { }
}
}
reader.Close();
}
catch (Exception ex) { }
finally
{
if (cmd != null) cmd.Dispose();
if (conn != null) { conn.Close(); conn.Dispose(); }
}

}

protected void Button1_Click1(object sender, EventArgs e)
{
string strConn = ConfigurationManager.ConnectionStrings[“ug_admission”].ConnectionString;
//”uid=MyUserId;pwd=MyPassword;server=MyPictureServer;database=MyDatabaseName;Connect Timeout=600″;
SqlConnection conn = null;
SqlCommand cmd = null;
//Int32 counter = 0;
byte[] imagedata = null;
Int32 intDesiredWidth = 250;
try
{
conn = new SqlConnection(strConn);
cmd = new SqlCommand();
cmd.Connection = conn;
//cmd.CommandTimeout = 60;
cmd.CommandType = CommandType.Text;
conn.Open();

//DataTable dt = ClsCommon.GetAdhocResult(“SELECT PHOTO, APPLICATION_SN FROM ug_applicant_general_info_PAID”).Tables[0];
DataTable dt = ClsCommon.GetAdhocResult(“SELECT PHOTO, APPLICATION_SN, SIGNATURE FROM ug_applicant_general_info_PAID”).Tables[0];

if(dt.Rows.Count>0)
{
foreach(DataRow dr in dt.Rows)
{
String Sl = dr[1].ToString();
imagedata = (byte[])dr[0];
System.Drawing.Image tmpOriginalImage = System.Drawing.Image.FromStream(new System.IO.MemoryStream(imagedata));
double dblScaleImg = (double)intDesiredWidth / (double)tmpOriginalImage.Width;
Graphics tmpGraphics = default(Graphics);
Bitmap tmpResizedImage = new Bitmap(Convert.ToInt32(dblScaleImg * tmpOriginalImage.Width), Convert.ToInt32(dblScaleImg * tmpOriginalImage.Height));
tmpGraphics = Graphics.FromImage(tmpResizedImage);
tmpGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
tmpGraphics.DrawImage(tmpOriginalImage, 0, 0, tmpResizedImage.Width + 1, tmpResizedImage.Height + 1);
//System.Drawing.Image imageOut = tmpResizedImage;
//System.Drawing.Image imageOut = tmpOriginalImage;
//imageOut.Save(“D:\\PicturesFolder\\SN_” + Sl + “.jpg”, System.Drawing.Imaging.ImageFormat.Jpeg); //counter++;

byte[] resizedImage = ImageToByte(tmpResizedImage);

//try
//{
// String qs = ” UPDATE ug_applicant_general_info_PAID SET PHOTO=@PHOTO WHERE APPLICATION_SN=@APPLICATION_SN “;
// SqlCommand cmd2 = new SqlCommand(qs, conn);
// cmd2.CommandType = CommandType.Text;
// cmd2.Parameters.Add(new SqlParameter(“@PHOTO”, resizedImage));
// cmd2.Parameters.Add(new SqlParameter(“@APPLICATION_SN”, Sl.Trim()));
// int result = cmd2.ExecuteNonQuery();
//}
//catch (Exception ex) { }

try
{
String qs = ” UPDATE ug_applicant_general_info_PAID SET PHOTO=@PHOTO WHERE APPLICATION_SN=@APPLICATION_SN “;
SqlCommand cmd2 = new SqlCommand(qs, conn);
cmd2.CommandType = CommandType.Text;
cmd2.Parameters.Add(new SqlParameter(“@PHOTO”, resizedImage));
cmd2.Parameters.Add(new SqlParameter(“@APPLICATION_SN”, Sl.Trim()));
int result = cmd2.ExecuteNonQuery();
}
catch (Exception ex) { }

// Signature
imagedata = (byte[])dr[2];
System.Drawing.Image tmpOriginalSig = System.Drawing.Image.FromStream(new System.IO.MemoryStream(imagedata));
double dblScaleSig = (double)intDesiredWidth / (double)tmpOriginalSig.Width;
Graphics tmpGraphicsSig = default(Graphics);
Bitmap tmpResizedSig = new Bitmap(Convert.ToInt32(dblScaleSig * tmpOriginalSig.Width), Convert.ToInt32(dblScaleSig * tmpOriginalSig.Height));
tmpGraphicsSig = Graphics.FromImage(tmpResizedSig);
tmpGraphicsSig.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
tmpGraphicsSig.DrawImage(tmpOriginalSig, 0, 0, tmpResizedSig.Width + 1, tmpResizedSig.Height + 1);

byte[] resizedSig = ImageToByte(tmpResizedSig);

try
{
String qs2 = ” UPDATE ug_applicant_general_info_PAID SET SIGNATURE=@SIGNATURE WHERE APPLICATION_SN=@APPLICATION_SN “;
SqlCommand cmd3 = new SqlCommand(qs2, conn);
cmd3.CommandType = CommandType.Text;
cmd3.Parameters.Add(new SqlParameter(“@SIGNATURE”, resizedSig));
cmd3.Parameters.Add(new SqlParameter(“@APPLICATION_SN”, Sl.Trim()));
int result = cmd3.ExecuteNonQuery();
}
catch (Exception ex) { }
}
}
}
catch (Exception ex) { }
finally
{
if (cmd != null) cmd.Dispose();
if (conn != null) { conn.Close(); conn.Dispose(); }
}

}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s