Ads by Lake Quincy Media Ads by Lake Quincy Media
Ads by Lake Quincy Media

Wednesday, October 28, 2009


Labels: , , ,

Resize Image in asp.net in C#

0 comments
This is a code in C# to resize an image. it takes two inputs as target image size and the image in byte format and it'll will return target sized image.

Some terms used in the code:-
Format24bppRgb - It specifies that formate is 24 bit per pixel and for each colour that is red, green and blue 8 bits are used



using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

public class ImageClass
{

private static byte[] ResizeImageFile(byte[] imageFile, int targetSize)
{
using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
{
Size newSize = CalculateDimensions(oldImage.Size, targetSize);//User defined function to calculate image dimensions
using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
{
using (Graphics canvas = Graphics.FromImage(newImage))
{
canvas.SmoothingMode = SmoothingMode.AntiAlias;//set rendering quality of canvas
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;//set interpolation mode to HighQualityBiubic
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));//drow image in canvas with new size and with specific starting location
MemoryStream m = new MemoryStream();
newImage.Save(m, ImageFormat.Jpeg);//define image formate in which it is to be shown
return m.GetBuffer();//return image as byte array
}
}
}
}

private static Size CalculateDimensions(Size oldSize, int targetSize)
{
Size newSize = new Size();

//change the size according to the widh and height
if (oldSize.Height > oldSize.Width)
{
newSize.Width = (int)(oldSize.Width * ((float)targetSize / (float)oldSize.Height));
newSize.Height = targetSize;
}
else
{
newSize.Width = targetSize;
newSize.Height = (int)(oldSize.Height * ((float)targetSize / (float)oldSize.Width));
}
return newSize;
}
}

Wednesday, September 30, 2009


Store PDF in sql server database in binary formate

0 comments
Using this code we can upload pdf file to sql database table in binary type field.


using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;

public class EmployeeData
{
public static void Main()
{
DateTime hireDate = DateTime.Parse("5/21/99");
AddEmployee("Jones", "Mary", "Sales Representative", hireDate, 5, "jones.bmp");
}

public static void AddEmployee(string lastName, string firstName, string title, DateTime hireDate , int reportsTo, string photoFilePath)
{
byte[] photo = GetPhoto(photoFilePath);

SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;");

SqlCommand addEmp = new SqlCommand("INSERT INTO Employees (LastName, FirstName, Title, HireDate, ReportsTo, Photo) " +
"Values(@LastName, @FirstName, @Title, @HireDate, @ReportsTo, @Photo)", nwindConn);

addEmp.Parameters.Add("@LastName", SqlDbType.NVarChar, 20).Value = lastName;
addEmp.Parameters.Add("@FirstName", SqlDbType.NVarChar, 10).Value = firstName;
addEmp.Parameters.Add("@Title", SqlDbType.NVarChar, 30).Value = title;
addEmp.Parameters.Add("@HireDate", SqlDbType.DateTime).Value = hireDate;
addEmp.Parameters.Add("@ReportsTo", SqlDbType.Int).Value = reportsTo;

addEmp.Parameters.Add("@Photo", SqlDbType.Image, photo.Length).Value = photo;

nwindConn.Open();

addEmp.ExecuteNonQuery();

nwindConn.Close();
}

public static byte[] GetPhoto(string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);

byte[] photo = br.ReadBytes((int)fs.Length);

br.Close();
fs.Close();

return photo;
}
}
Ads by Lake Quincy Media