Front Code:
<asp:fileupload id="FileUpload1" runat="server" /> <asp:button id="Button2" runat="server" text= " butto2n "onclick="button2_click "/>
protected void button2_click ( sender, EventArgs e) {
if ( Fileupload1.hasfiles) {
[] pbyte = Fileupload1.filebytes;
string type = //Verify input image size
byte [] newpbyte = Val Idatepicture (pbyte, type);
//Save picture The first parameter is the picture name, if the picture exists in the data then you can use the image ID to specify a name such as 101.jpeg
Savepictureinimageserver ( 101 , newpbyte, type); }}
public void Savepictureinimageserver (int pictureid, byte[] picturebinary, string mimeType)
{
string[] parts = mimetype.split ('/');
String Lastpart = Parts[parts. LENGTH-1];
Switch (Lastpart)
{
Case "Pjpeg":
Lastpart = "JPG";
Break
Case "X-png":
Lastpart = "png";
Break
Case "X-icon":
Lastpart = "ico";
Break
}
Picture name
String localfilename = String. Format ("{0}.{ 1} ", Pictureid.tostring (), Lastpart);
Create file save to storage path
File.writeallbytes (Path.Combine (Localimagepath, LocalFilename), picturebinary);
}
Get Picture Storage Path
public string Localimagepath
{
Get
{
String path = HttpContext.Current.Request.PhysicalApplicationPath + "Images";
return path;
}
}
Verify the input image size
Public byte[] Validatepicture (byte[] picturebinary, string mimeType)
{
using (MemoryStream stream = new MemoryStream (picturebinary))
{
var B = new Bitmap (stream);
int maxSize = 600;//The maximum size of the picture is set here.
if ((B.height > maxSize) | | (B.width > MaxSize))
{
Calculate image dimensions while maintaining vertical and horizontal
var newSize = calculatedimensions (b.size, maxSize);
var newbitmap = new Bitmap (newsize.width, newsize.height);
var g = graphics.fromimage (Newbitmap);
G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
G.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.compositingquality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
G.pixeloffsetmode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
G.drawimage (b, 0, 0, newsize.width, newsize.height);
var m = new MemoryStream ();
var EP = new EncoderParameters ();
Ep. Param[0] = new Encoderparameter (System.Drawing.Imaging.Encoder.Quality, 100L); 100l-picture sharpness
ImageCodecInfo ici = Getimagecodecinfofrommimetype (MimeType);//Returns the first image codec information for a specified type.
if (ici = = null)
ICI = Getimagecodecinfofrommimetype ("Image/jpeg");
Newbitmap.save (M, ICI, EP);
Newbitmap.dispose ();
B.dispose ();
return M.getbuffer ();
}
Else
{
B.dispose ();
return picturebinary;
}
}
}
Calculate image dimensions while maintaining vertical and horizontal
Private size calculatedimensions (size originalsize, int targetsize)
{
var newSize = new Size ();
if (Originalsize.height > Originalsize.width)//Portrait
{
Newsize.width = (int) (Originalsize.width * (float) (Targetsize/(float) originalsize.height));
Newsize.height = targetsize;
}
Else
{
Newsize.height = (int) (Originalsize.height * (float) (Targetsize/(float) originalsize.width));
Newsize.width = targetsize;
}
return newSize;
}
Returns the first image codec information for a specified type.
Private ImageCodecInfo Getimagecodecinfofrommimetype (string mimeType)
{
var info = imagecodecinfo.getimageencoders ();
foreach (var ici in info)
if (ICI. Mimetype.equals (MimeType, StringComparison.OrdinalIgnoreCase))
return ICI;
return null;
}
<pre></pre>
<p> </p>
<p> North Qing Paper Net-China's leading graduate thesis website </p>
<p> "<a href=" http://www.bqlww.com/"> North Qing Paper Net </a>" "<a href=" http://www.51byl.net/"> I'm graduating. Thesis net </a> "</p>
. NET upload a picture instance