Code to save image and create thumbnails
The code is relatively easy to follow, but I suggest that it is read in conjunction with
the original article at
SQL Junkies - Creating and Saving Images to a Database in ASP.NET,
which is in VB.net. I implemented the code using the concept of static methods and a stored procedure
to update the code.
using ... // Default list of resources not shown
using System.Drawing;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data.Common;
using System.IO;
using System.Drawing.Imaging;
using System.Diagnostics;
public class ImageMangement
{
public static void ImageSave(FileUpload ImageToUpLoad, int dis_id)
{
HttpPostedFile selectedFile = ImageToUpLoad.PostedFile;
if (selectedFile.ContentLength > 0 && selectedFile.ContentType.Length
> 0)
{
// Read the data of the image into a blob;
Stream imgStream = selectedFile.InputStream;
byte[] binaryImageData = new byte[selectedFile.ContentLength];
imgStream.Read(binaryImageData, 0, selectedFile.ContentLength);
//We need to create a thumbnail of the
image that has been selected
Byte[] binaryImageDataThumbnail =
createThumnail(imgStream, 145, 145);
// Create a new command that will be run
in order to create the image
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["AspUserMangement"].ConnectionString);
try
{
using (con)
{
SqlCommand
cmd = new SqlCommand("jtb_DisplayImage_Add", con);
cmd.CommandType
= CommandType.StoredProcedure;
SqlParameter
Param0 = new SqlParameter("@dis_ID", SqlDbType.Int);
SqlParameter
Param1 = new SqlParameter("@img_name", SqlDbType.VarChar, 128);
SqlParameter
Param2 = new SqlParameter("@img_Type", SqlDbType.VarChar, 128);
SqlParameter
Param3 = new SqlParameter("@img_SmallImage", SqlDbType.Image);
SqlParameter
Param4 = new SqlParameter("@img_LargeImage", SqlDbType.Image);
Param0.Value
= dis_id;
Param1.Value
= ImageToUpLoad.PostedFile.FileName.Substring(ImageToUpLoad.PostedFile.FileName.LastIndexOf("\\")
+ 1);
Param2.Value
= selectedFile.ContentType;
Param3.Value
= binaryImageDataThumbnail;
Param4.Value
= binaryImageData;
cmd.Parameters.Add(Param0);
cmd.Parameters.Add(Param1);
cmd.Parameters.Add(Param2);
cmd.Parameters.Add(Param3);
cmd.Parameters.Add(Param4);
//// Save
the image
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
System.Diagnostics.Debug.WriteLine(ex.StackTrace);
throw (ex);
}
finally
{
if (con != null)
con.Close();
}
}
}
public static byte[] createThumnail(Stream originalImage, double width,
double height)
{
System.Drawing.Image g = System.Drawing.Image.FromStream(originalImage);
Size thumbSize = new Size();
thumbSize = NewthumbSize(g.Width, g.Height, width, height);
Bitmap imgOutput = new Bitmap(g, thumbSize.Width, thumbSize.Height);
MemoryStream imgStream = new MemoryStream();
ImageFormat thisformat = g.RawFormat;
imgOutput.Save(imgStream, thisformat);
byte[] imgbin = new byte[imgStream.Length];
imgStream.Position = 0;
imgStream.Read(imgbin, 0, imgbin.Length);
g.Dispose();
imgOutput.Dispose();
return imgbin;
}
public static Size NewthumbSize(double currentwidth, double currentheight,
double newWidth, double newHeight)
{
// Calculate the Size of the New image
double tempMultiplier = 1;
if (currentheight > currentwidth)
{
tempMultiplier = newHeight / currentheight;
}
else
{
tempMultiplier = newWidth / currentwidth;
}
Size NewSize = new Size((int)(currentwidth * tempMultiplier),
(int)(currentheight * tempMultiplier));
return NewSize;
}
For completeness the SQL query used above is:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[jtb_DisplayIMage_Add]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[jtb_DisplayImage_Add]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[jtb_DisplayImage_Add]
@dis_ID int,
@img_name varchar (128),
@img_type varchar (128),
@img_SmallImage image,
@img_LargeImage image
AS
BEGIN
SET NOCOUNT ON
insert into DisplayImage
(Dis_ID, img_Name, img_Type, img_SmallImage, img_largeImage )
values (@dis_Id, @img_Name, @img_Type, @img_SmallImage, @img_largeImage )
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
|