asp.net| upload | upload pictures | thumbnails in asp.net upload pictures and generate thumbnails of C # source code
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using System.IO;
Using System.Drawing.Imaging;
Namespace Emeng.exam
{
<summary>
Summary description of the Thumbnail.
</summary>
public class Thumbnail:System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
Label1.Text = "Button1.Text = "Upload and display thumbnails";
}
Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Button1.Click + = new System.EventHandler (this. Button1_Click);
This. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
private void Button1_Click (object sender, System.EventArgs e)
{
Httpfilecollection myfilecoll = HttpContext.Current.Request.Files;
Httppostedfile mypostedfile = myfilecoll[0];
if (MyPostedFile.ContentType.ToString (). ToLower (). IndexOf ("image") < 0)
{
Response.Write ("Invalid graphic format.") ");
Return
}
GetThumbnail (mypostedfile.filename, 100, 100,
MyPostedFile.ContentType.ToString (), false, Mypostedfile.inputstream);
}
Private System.Drawing.Imaging.ImageFormat Getimagetype (object Strcontenttype)
{
if (strcontenttype.tostring (). ToLower ()) = = "Image/pjpeg")
{
return System.Drawing.Imaging.ImageFormat.Jpeg;
}
else if (strcontenttype.tostring (). ToLower ()) = = "Image/gif")
{
return System.Drawing.Imaging.ImageFormat.Gif;
}
else if (strcontenttype.tostring (). ToLower ()) = = "Image/bmp")
{
return SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.BMP;
}
else if (strcontenttype.tostring (). ToLower ()) = = "Image/tiff")
{
return System.Drawing.Imaging.ImageFormat.Tiff;
}
else if (strcontenttype.tostring (). ToLower ()) = = "Image/x-icon")
{
return System.Drawing.Imaging.ImageFormat.Icon;
}
else if (strcontenttype.tostring (). ToLower ()) = = "Image/x-png")
{
return System.Drawing.Imaging.ImageFormat.Png;
}
else if (strcontenttype.tostring (). ToLower ()) = = "IMAGE/X-EMF")
{
return SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.EMF;
}
else if (strcontenttype.tostring (). ToLower ()) = = "Image/x-exif")
{
return System.Drawing.Imaging.ImageFormat.Exif;
}
else if (strcontenttype.tostring (). ToLower ()) = = "Image/x-wmf")
{
return SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.WMF;
}
Else
{
return SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.MEMORYBMP;
}
}
private void GetThumbnail (string strfilename, int iwidth, int iheight,
String Strcontenttype, bool Blngetfromfile, System.IO.Stream Imgstream)
{
System.Drawing.Image oimg;
if (blngetfromfile)
{
oimg = System.Drawing.Image.FromFile (strFileName);
}
Else
{
oimg = System.Drawing.Image.FromStream (Imgstream);
}
oimg = Oimg.getthumbnailimage (iwidth, iheight, NULL, IntPtr.Zero);
String strguid = System.Guid.NewGuid (). ToString (). ToUpper ();
String strfileext = Strfilename.substring (Strfilename.lastindexof ("."));
Response.ContentType = Strcontenttype;
MemoryStream memstream = new MemoryStream ();
Oimg.save (Memstream, Getimagetype (Strcontenttype));
Memstream.writeto (Response.outputstream);
}
}
}
[1] [2] Next page