1. Create
a Table in your Database
CREATE TABLE
[dbo].[Photos](
[PhotoID] [int] IDENTITY(1,1)
NOT NULL,
[UserName] [nvarchar](50)
NOT NULL,
[Caption] [nvarchar](50) NOT NULL,
[ImageOriginal] [image]
NOT NULL,
[ImageSmall]
[image] NOT
NULL,
CONSTRAINT [PK_Photos]
PRIMARY KEY CLUSTERED
([PhotoID] ASC)
WITH (PAD_INDEX
= OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON
[PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
2.
Add a Property to your Page code behind
for holding the UserName
private
string _username;
public string UserName
{
get {
return _username; }
set { _username = value; }
}
3.
On Page_Load event get the Current Online
User’s UserName
protected void
Page_Load(object
sender, EventArgs
e{
System.Web.Security.MembershipUser ThisUser =
Membership.GetUser();
if (ThisUser !=
null)
{
UserName = ThisUser.UserName;
}
else
UserName = string.Empty;
}
4.
Add a new Proceedure to your Database
CREATE PROCEDURE
[dbo].[AddPhoto]
@UserName nvarchar(50),
@Caption nvarchar(50),
@ImageOriginal
image,
@ImageSmall image
AS
INSERT INTO [Photos] (
[UserName],
[Caption],
[ImageOriginal],
[ImageSmall]
)
VALUES (
@UserName,
@Caption,
@ ImageOriginal,
@ ImageSmall
)
RETURN
5.
Create a method for adding a picture
to your Datebase
public
static void AddUserPicToUsersPics (string UserName,
string Caption,
byte[] BytesOriginal)
{
using (SqlConnection connection = new
SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
{
using (SqlCommand command =new
SqlCommand("AddPhoto", connection))
{
command.CommandType =
CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@UserName", UserName));
command.Parameters.Add(new SqlParameter("@Caption", Caption));
command.Parameters.Add(new
SqlParameter("@ImageOriginal", BytesOriginal));
command.Parameters.Add(new SqlParameter("@ImageSmall",
ResizeImageFile(BytesOriginal, 100)));
connection.Open();
command.ExecuteNonQuery();
}
}
}
6.
Create a Method which will create a
Thumbnail view of the image
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);
using (Bitmap newImage = new
Bitmap(newSize.Width, newSize.Height,
PixelFormat.Format24bppRgb))
{
using
(Graphics canvas
= Graphics.FromImage(newImage))
{
canvas.SmoothingMode
= SmoothingMode.AntiAlias;
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
canvas.DrawImage(oldImage, new
Rectangle(new
Point(0, 0), newSize));
MemoryStream m = new
MemoryStream();
newImage.Save(m,
ImageFormat.Jpeg);
return
m.GetBuffer();
}
}
}
}
7.
Create a Helper Method for calculating
the dimension for Thumbnail Image
private
static Size CalculateDimensions(Size oldSize,
int targetSize)
{
Size newSize =
new
Size();
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;
}
8.
Add a FileUpload Web Control and a Button
to your Page
<asp:FileUpload ID="FileUpload1"
runat="server" Width="400px" />
<br />
<asp:Button ID="btnSave" runat="server" Text="Save
Image" OnClick="btnSave_Click"
/>
9.
Add code for saving image to the database
on a Button Click event