imageSave(fluCatImage.FileName, fluCatImage.PostedFile.InputStream, Server.MapPath("~/"), Server.MapPath("../uploadimage/"));
//Implement Function
public void imageSave(string imagename, Stream s, string serverpath, string foldername)
{
if (imagename != string.Empty)
{
System.Drawing.Image image = System.Drawing.Image.FromStream(s);
Bitmap source = new Bitmap(image); //<-- or any other source
Bitmap target = new Bitmap(source.Width, source.Height);
Graphics g = Graphics.FromImage(target);
EncoderParameters e;
g.CompositingQuality = CompositingQuality.HighSpeed; //<-- here
g.InterpolationMode = InterpolationMode.Low;// <-- here
Rectangle recCompression = new Rectangle(0, 0, source.Width, source.Height);
g.DrawImage(source, recCompression);
e = new EncoderParameters(2);
e.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)70);// <-- here 70% quality
e.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW); //<-- here
DateTime MyDate = DateTime.Now;
source.Dispose();
string imgname = DateTime.UtcNow.ToString().Replace(" ", "").Replace("AM", "").Replace("PM", "").Replace("/", "").Replace("-", "").Replace(":", "");
String MyString = imgname + ".jpg";
target.Save(foldername + MyString, GetEncoderInfo("image/jpeg"), e);
// MemoryStream ms = new MemoryStream();
// target.Save(ms, GetEncoderInfo("image/jpeg"),e);
// ms.WriteTo(ms);
g.Dispose();
target.Dispose();
}
}
public static ImageCodecInfo GetEncoderInfo(string sMime)
{
ImageCodecInfo[] objEncoders;
objEncoders = ImageCodecInfo.GetImageEncoders();
for (int iLoop = 0; iLoop <= (objEncoders.Length - 1); iLoop++)
{
if (objEncoders[iLoop].MimeType == sMime)
return objEncoders[iLoop];
}
return null;
}
0 comments:
Post a Comment