As shown in figure, through the file input to upload a file, how to compress and upload
Don't come to XXX. The Save (), here does not support directly to Save, is uploaded to the file server, so be compressed into steam directly uploaded
CodePudding user response:
https://blog.csdn.net/starfd/article/details/54575199The bottom compression code, then Save to a local, or to Stream, is to call the Save Image method
CodePudding user response:
Do you want to upload after compression, it will use JS, are first upload again compression in c #,CodePudding user response:
Online a lot all write too complicated, here he compiled a likeIf (System. Web. HttpContext. Current. Request. The Files. The Count==0)
{
Throw new Exception (" no select files!" );
}
//takes pictures from the front desk to upload here
Var file=System. Web. HttpContext. Current. Request. The Files [0].
//file will be compressed ratio (file. ContentLength is the size of the current file, and here the default file compression as 1024 KB, can adjust)
Double compressionRatio=1024 * 1024/Convert ToDouble (file. ContentLength);
CompressionRatio=Math. Round (compressionRatio, 2);
//upload a file into a byte array
Byte [] fileByte=new byte [file ContentLength];
File. The InputStream. Read (fileByte, 0, the file. The ContentLength);
//upload files byte array into a Stream
MemoryStream ms=new MemoryStream (fileByte);
Image img=Image. FromStream (ms);
//proportionately new wide high
Int toWidth=the Convert. ToInt32 (img. Width * compressionRatio);
Int toHeight=the Convert. ToInt32 (img. Height * compressionRatio);
//in accordance with the new high with the canvas to draw a wide
Bitmap Bitmap=new Bitmap (toWidth toHeight);
Graphics g=Graphics. FromImage (bitmap);
G.I nterpolationMode=System. Drawing. Drawing2D. InterpolationMode. High;
G.S moothingMode=System. Drawing. Drawing2D. SmoothingMode. HighQuality;
Right to Lear (System. Drawing. Color. Transparent);
G.D rawImage (img, new System, Drawing a Rectangle (0, 0, toWidth, toHeight), the new System. Drawing. The Rectangle (0, 0, img Width, img. Height), System. Drawing. The GraphicsUnit. Pixel);
//to draw good bitmap into a stream (not necessarily time-consuming stream, byte array can be anything)
Var fileStream=new MemoryStream ();
Using (MemoryStream stream=new MemoryStream ())
{
Bitmap. The Save (stream, ImageFormat. Png);
Byte [] data=https://bbs.csdn.net/topics/new byte [stream Length];
Stream. Seek (0, SeekOrigin. Begin);
Stream. Read (data, 0, Convert ToInt32 (stream. Length));
FileStream=new MemoryStream (data);
}
//at this point, get a good compression stream or bytes or upload or front desk display is ok (# ^. ^ #)
CodePudding user response:
Upload images into image format into the first parameterThe generated image address into the second parameter
Set the compression ratio (1 to 100) was introduced into the third argument
Implementation before uploading compression quality basic condition
Public static bool CompressImage (System. Drawing. The Image sFile, string dFile, int flag=90)
{
System. Drawing. The Image iSource=sFile;
ImageFormat tFormat=iSource. RawFormat;
Int dHeight=iSource. Height/2;
Int dWidth=iSource. Width/2;
Int sW=0, sH=0;
//scaling
The Size tem_size=new Size (iSource. Width, iSource. Height);
If (tem_size. Width & gt; DHeight | | tem_size. Width & gt; DWidth)
{
If ((tem_size Width * dHeight) & gt; (tem_size. Width * dWidth))
{
SW=dWidth;
SH=(dWidth * tem_size. Height)/tem_size Width;
}
The else
{
SH=dHeight;
SW=(tem_size Width * dHeight)/tem_size Height;
}
}
The else
{
SW=tem_size. Width;
SH=tem_size. Height;
}
Bitmap ob=new Bitmap (dWidth dHeight);
Graphics g=Graphics. FromImage (ob);
Right to Lear (Color. WhiteSmoke);
G.Com positingQuality=System.Drawing.Drawing2D.Com positingQuality. HighQuality;
G.S moothingMode=System. Drawing. Drawing2D. SmoothingMode. HighQuality;
G.I nterpolationMode=System. Drawing. Drawing2D. InterpolationMode. HighQualityBicubic;
G.D rawImage (iSource, new Rectangle ((dWidth - sW)/2, (dHeight - sH)/2, sW, sH), 0, 0, iSource. Width, iSource. Height, GraphicsUnit. Pixel);
G.D ispose ();
//the code below to save images, set the compression quality
EncoderParameters ep=new EncoderParameters ();
Long [] qy=new long [1].
Qy [0]=flag;//set the compression ratio of 1-100
EncoderParameter eParam=new EncoderParameter (System. Drawing. Imaging. Encoder. Quality, qy);
Ep. Param [0]=eParam;
Try
{
ImageCodecInfo [] arrayICI=ImageCodecInfo. GetImageEncoders ();
ImageCodecInfo jpegICIinfo=null;
For (int x=0; X & lt; ArrayICI. Length; X++)
{
If (arrayICI [x]) FormatDescription) Equals (" JPEG "))
{
JpegICIinfo=arrayICI [x];
break;
}
}
If (jpegICIinfo!=null)
{
Ob. Save (dFile jpegICIinfo, ep);//dFile is a new path of compressed
The FileInfo fi=new the FileInfo (dFile);
}
The else
{
Ob. Save (dFile tFormat);
}
return true;
}
Catch
{
return false;
}
The finally
{
ISource. The Dispose ();
Ob. The Dispose ();
}
}
CodePudding user response:
https://gitee.com/bibaoke/Less.Image the image cutting project, I wrotenullnullnullnullnull