C # source code for uploading images and generating thumbnails in asp.net

Source: Internet
Author: User
Tags tostring
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



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.