This tutorial will show how we use the FileUpload Control to upload a file to the website, whilst simultaneously creating a thumbnail of the image, and saving that too. C# version.
In this tutorial, we will create a single file upload form and a GridView to display the list of uploaded files.
The upload form will upload a single file at a time, when the user click the button, and it will also create a thumbnail of the image, saving both images to the uploads folder.
Add AddImages.aspx
Design side
.............................................................................................................................................
<table width="100%" cellspacing="5">
<tr>
<td>
<h3>
Add Images</h3>
</td>
<td>
</td>
</tr>
<tr>
<td width="120px" colspan="2">
<asp:Label ID="lbl_msg" runat="server" ForeColor="Red"></asp:Label>
</td>
</tr>
</tr>
<tr>
<td>
Image Name :
</td>
<td>
<asp:TextBox ID="txtFilename" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
New Image:
</td>
<td>
<asp:FileUpload ID="fupAddVideo" runat="server" />
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btn_Save" runat="server" OnClick="btn_Save_Click" Text="Save" OnClientClick="return ValidateControls()"
Width="80px" />
</td>
</tr>
</table>
...............................................................................................................................................
In the code behind
........................................................................................................................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DC;
using BL;
using System.IO;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.Data;
public partial class AddImages : System.Web.UI.Page
{
string postedfilename, SavePath, Filenamewithpath;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void btn_Save_Click(object sender, EventArgs e)
{
if (fupAddVideo != null || fupAddVideo.PostedFile != null)
{
postedfilename = fupAddVideo.PostedFile.FileName;
SavePath = AppDomain.CurrentDomain.BaseDirectory + "Images\\" + "Photos\\";
string NewFName = postedfilename;
NewFName = NewFName.Substring(NewFName.LastIndexOf("\\") + 1, NewFName.Length - NewFName.LastIndexOf(".")) + "." + NewFName.Substring(NewFName.LastIndexOf(".") + 1);
Filenamewithpath = SavePath + NewFName;
//Save The file
fupAddVideo.PostedFile.SaveAs(Filenamewithpath);
//Start Converting
string outputfile;
string withoutext;
//Get the file name without Extension
withoutext = Path.GetFileNameWithoutExtension(Filenamewithpath);
In this tutorial, we will create a single file upload form and a GridView to display the list of uploaded files.
The upload form will upload a single file at a time, when the user click the button, and it will also create a thumbnail of the image, saving both images to the uploads folder.
Add AddImages.aspx
Design side
.............................................................................................................................................
<table width="100%" cellspacing="5">
<tr>
<td>
<h3>
Add Images</h3>
</td>
<td>
</td>
</tr>
<tr>
<td width="120px" colspan="2">
<asp:Label ID="lbl_msg" runat="server" ForeColor="Red"></asp:Label>
</td>
</tr>
</tr>
<tr>
<td>
Image Name :
</td>
<td>
<asp:TextBox ID="txtFilename" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
New Image:
</td>
<td>
<asp:FileUpload ID="fupAddVideo" runat="server" />
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btn_Save" runat="server" OnClick="btn_Save_Click" Text="Save" OnClientClick="return ValidateControls()"
Width="80px" />
</td>
</tr>
</table>
...............................................................................................................................................
In the code behind
........................................................................................................................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DC;
using BL;
using System.IO;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.Data;
public partial class AddImages : System.Web.UI.Page
{
string postedfilename, SavePath, Filenamewithpath;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void btn_Save_Click(object sender, EventArgs e)
{
if (fupAddVideo != null || fupAddVideo.PostedFile != null)
{
postedfilename = fupAddVideo.PostedFile.FileName;
SavePath = AppDomain.CurrentDomain.BaseDirectory + "Images\\" + "Photos\\";
string NewFName = postedfilename;
NewFName = NewFName.Substring(NewFName.LastIndexOf("\\") + 1, NewFName.Length - NewFName.LastIndexOf(".")) + "." + NewFName.Substring(NewFName.LastIndexOf(".") + 1);
Filenamewithpath = SavePath + NewFName;
//Save The file
fupAddVideo.PostedFile.SaveAs(Filenamewithpath);
//Start Converting
string outputfile;
string withoutext;
//Get the file name without Extension
withoutext = Path.GetFileNameWithoutExtension(Filenamewithpath);
outputfile = SavePath + withoutext + ".JPG";
Session["outputfile"] = withoutext + ".JPG";
//Creating Thumbnail
System.Drawing.Image image = System.Drawing.Image.FromStream(fupAddVideo.PostedFile.InputStream);
float imgWidth = image.PhysicalDimension.Width;
float imgHeight = image.PhysicalDimension.Height;
float imgSize = imgHeight > imgWidth ? imgHeight : imgWidth;
float imgResize = imgSize <= 128 ? (float)1.0 : 128 / imgSize;
imgWidth *= imgResize; imgHeight *= imgResize;
System.Drawing.Image thumb = image.GetThumbnailImage((int)imgWidth, (int)imgHeight, delegate() { return false; }, (IntPtr)0);
string ThumbName =AppDomain.CurrentDomain.BaseDirectory + "Images\\"+"Thumb\\";
string thumbpath;
thumbpath = ThumbName + withoutext + "1" + ".jpg";
if (File.Exists(thumbpath))
File.Delete(thumbpath);
thumb.Save(thumbpath);
Session["thumbname"] = withoutext + "1" + ".jpg";
....................................................................................................................

8 comments:
Good article
You can submit your .net related article links on http://www.dotnettechy.com to get more traffic
Thanks micle.
hi, vikram
how do you add the name spaces DC & BL, can you just explain..
thanks
ali
how do you add the name spaces DC & BL, its not taking these namespaces...Nabi Bux Gabol
@nb gabol & ali shameer
You need to create a class file in the AppData folder.
what to add in that class file??
How to generate thumbnail from uploaded video file
How to generate thumbnail from uploaded video file in c#
Post a Comment