Resize Image Before/And Upload To Databse ASP.NET
In this example i am going to describe how to resize image in ASP.NET before/and upload to ms sql database using C# and Vb.NET.
For this i am using FileUpload control to upload the image in datbase after resizing.
I am also displaying the Image in Gridviw after uploading to database.
To know how to display images in gridview read Display Images In GridView From DataBase
ASPX page html markup look like this
Hope this helps
In this example i am going to describe how to resize image in ASP.NET before/and upload to ms sql database using C# and Vb.NET.
For this i am using FileUpload control to upload the image in datbase after resizing.
I am also displaying the Image in Gridviw after uploading to database.
To know how to display images in gridview read Display Images In GridView From DataBase
ASPX page html markup look like this
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<br />
<asp:Button ID="btnUpload" runat="server"
OnClick="btnUpload_Click" Text="Upload" />
<br />
<br />
<asp:Label ID="lblMessage" runat="server"></asp:Label>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"
InsertVisible="False" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="ImageName" HeaderText="ImageName"
SortExpression="ImageName" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# "Handler.ashx?ID=" + Eval("ID")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [ImageName], [Image]
FROM [Images]"></asp:SqlDataSource>
</div>
</form>
C# Code Behind
01
using
System;
02
using
System.Data;
03
using
System.Configuration;
04
using
System.Web;
05
using
System.Web.Security;
06
using
System.Web.UI;
07
using
System.Web.UI.WebControls;
08
using
System.Web.UI.WebControls.WebParts;
09
using
System.Web.UI.HtmlControls;
10
using
System.IO;
11
using
System.Data.SqlClient;
12
using
System.Drawing.Imaging;
13
using
System.Drawing.Drawing2D;
14
using
System.Drawing;
15
16
17
public
partial
class
_Default : System.Web.UI.Page
18
{
19
protected
void
Page_Load(
object
sender, EventArgs e)
20
{
21
22
}
23
protected
void
btnUpload_Click(
object
sender, EventArgs e)
24
{
25
string
strImageName = txtName.Text.ToString();
26
if
(FileUpload1.PostedFile !=
null
&& FileUpload1.PostedFile.FileName !=
""
)
27
{
28
string
strExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
29
if
((strExtension.ToUpper() ==
".JPG"
) | (strExtension.ToUpper() ==
".GIF"
))
30
{
31
// Resize Image Before Uploading to DataBase
32
System.Drawing.Image imageToBeResized = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
33
int
imageHeight = imageToBeResized.Height;
34
int
imageWidth = imageToBeResized.Width;
35
int
maxHeight = 240;
36
int
maxWidth = 320;
37
imageHeight = (imageHeight * maxWidth) / imageWidth;
38
imageWidth = maxWidth;
39
40
if
(imageHeight > maxHeight)
41
{
42
imageWidth = (imageWidth * maxHeight) / imageHeight;
43
imageHeight = maxHeight;
44
}
45
46
Bitmap bitmap =
new
Bitmap(imageToBeResized, imageWidth, imageHeight);
47
System.IO.MemoryStream stream =
new
MemoryStream();
48
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
49
stream.Position = 0;
50
byte
[] image =
new
byte
[stream.Length + 1];
51
stream.Read(image, 0, image.Length);
52
53
54
55
// Create SQL Connection
56
SqlConnection con =
new
SqlConnection();
57
con.ConnectionString = ConfigurationManager.ConnectionStrings[
"ConnectionString"
].ConnectionString;
58
59
// Create SQL Command
60
61
SqlCommand cmd =
new
SqlCommand();
62
cmd.CommandText =
"INSERT INTO Images(ImageName,Image) VALUES (@ImageName,@Image)"
;
63
cmd.CommandType = CommandType.Text;
64
cmd.Connection = con;
65
66
SqlParameter ImageName =
new
SqlParameter(
"@ImageName"
, SqlDbType.VarChar, 50);
67
ImageName.Value = strImageName.ToString();
68
cmd.Parameters.Add(ImageName);
69
70
SqlParameter UploadedImage =
new
SqlParameter(
"@Image"
, SqlDbType.Image, image.Length);
71
UploadedImage.Value = image;
72
cmd.Parameters.Add(UploadedImage);
73
con.Open();
74
int
result = cmd.ExecuteNonQuery();
75
con.Close();
76
if
(result > 0)
77
lblMessage.Text =
"File Uploaded"
;
78
GridView1.DataBind();
79
}
80
}
81
}
82
}
VB.NET Code Behind
01
Imports
System
02
Imports
System.Data
03
Imports
System.Configuration
04
Imports
System.Web
05
Imports
System.Web.Security
06
Imports
System.Web.UI
07
Imports
System.Web.UI.WebControls
08
Imports
System.Web.UI.WebControls.WebParts
09
Imports
System.Web.UI.HtmlControls
10
Imports
System.IO
11
Imports
System.Data.SqlClient
12
Imports
System.Drawing.Imaging
13
Imports
System.Drawing.Drawing2D
14
Imports
System.Drawing
15
16
17
Public
Partial
Class
_Default
18
Inherits
System.Web.UI.Page
19
Protected
Sub
Page_Load(
ByVal
sender
As
Object
,
ByVal
e
As
EventArgs)
20
21
End
Sub
22
Protected
Sub
btnUpload_Click(
ByVal
sender
As
Object
,
ByVal
e
As
EventArgs)
23
Dim
strImageName
As
String
= txtName.Text.ToString()
24
If
FileUpload1.PostedFile IsNot
Nothing
AndAlso
FileUpload1.PostedFile.FileName <>
""
Then
25
Dim
strExtension
As
String
= System.IO.Path.GetExtension(FileUpload1.FileName)
26
If
(strExtension.ToUpper() =
".JPG"
)
Or
(strExtension.ToUpper() =
".GIF"
)
Then
27
' Resize Image Before Uploading to DataBase
28
Dim
imageToBeResized
As
System.Drawing.Image = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream)
29
Dim
imageHeight
As
Integer
= imageToBeResized.Height
30
Dim
imageWidth
As
Integer
= imageToBeResized.Width
31
Dim
maxHeight
As
Integer
= 240
32
Dim
maxWidth
As
Integer
= 320
33
imageHeight = (imageHeight * maxWidth) / imageWidth
34
imageWidth = maxWidth
35
36
If
imageHeight > maxHeight
Then
37
imageWidth = (imageWidth * maxHeight) / imageHeight
38
imageHeight = maxHeight
39
End
If
40
41
Dim
bitmap
As
New
Bitmap(imageToBeResized, imageWidth, imageHeight)
42
Dim
stream
As
System.IO.MemoryStream =
New
MemoryStream()
43
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
44
stream.Position = 0
45
Dim
image
As
Byte
() =
New
Byte
(stream.Length) {}
46
stream.Read(image, 0, image.Length)
47
48
49
50
' Create SQL Connection
51
Dim
con
As
New
SqlConnection()
52
con.ConnectionString = ConfigurationManager.ConnectionStrings(
"ConnectionString"
).ConnectionString
53
54
' Create SQL Command
55
56
Dim
cmd
As
New
SqlCommand()
57
cmd.CommandText =
"INSERT INTO Images(ImageName,Image) VALUES (@ImageName,@Image)"
58
cmd.CommandType = CommandType.Text
59
cmd.Connection = con
60
61
Dim
ImageName
As
New
SqlParameter(
"@ImageName"
, SqlDbType.VarChar, 50)
62
ImageName.Value = strImageName.ToString()
63
cmd.Parameters.Add(ImageName)
64
65
Dim
UploadedImage
As
New
SqlParameter(
"@Image"
, SqlDbType.Image, image.Length)
66
UploadedImage.Value = image
67
cmd.Parameters.Add(UploadedImage)
68
con.Open()
69
Dim
result
As
Integer
= cmd.ExecuteNonQuery()
70
con.Close()
71
If
result > 0
Then
72
lblMessage.Text =
"File Uploaded"
73
End
If
74
GridView1.DataBind()
75
End
If
76
End
If
77
End
Sub
78
End
Class
0 comments:
Post a Comment