Code
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Net;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Drawing;
#region 图片处理
protected void Button3_Click(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(Server.MapPath("/Upload"));
FileSystemInfo[] dis = di.GetFileSystemInfos();
if (dis.Length < 1)
{
}
else
{
for (int i = 0; i < dis.Length; i++) {
//修改文件名
string path = Server.MapPath("/Upload/") + dis[i].ToString();
//修改该文件夹下的文件夹名
string logopath = path + "/logo";
DirectoryInfo di3 = new DirectoryInfo(logopath);
if (di3.Exists) {
string logopathnew = path + "/Brand";
try
{
di3.MoveTo(logopathnew);
}
catch { }
}
string Productpath = path + "/产品";
DirectoryInfo di4 = new DirectoryInfo(Productpath);
if (di4.Exists) {
string Productpath4 = path + "/product";
try
{
di4.MoveTo(Productpath4);
}
catch { }
}
//生成产品缩图
Productpath = path + "/Product";
DirectoryInfo di5 = new DirectoryInfo(Productpath);
if (di5.Exists) {
FileSystemInfo[] fis = di5.GetFileSystemInfos();
if (fis.Length > 1) {
for (int j = 0; j < fis.Length; j++) {
if (File.Exists(Productpath + "/" + fis[j].ToString()))
{
ReducedThumbnail(Productpath + "/" + fis[j].ToString(), Productpath + "/S-" + fis[j].ToString(), 115, 104);
}
}
}
}
//修改该文件夹的名字
DirectoryInfo di2 = new DirectoryInfo(path);
if (di2.Exists)
{
string path1 = Server.MapPath("/Upload/") + GetEnterpriseIdByName2(dis[i].ToString());
try
{
di2.MoveTo(path1);
}
catch { }
}
}
}
}
public string GetEnterpriseIdByName2(string Entname) {
//List<XbEnterprise> EnterList = new List<XbEnterprise>();
List<XbAccount> Acclist = new List<XbAccount>();
List<string> ChangeStr = new List<string>();
ChangeStr.Add("LoginID='" + Entname + "'");
Acclist = XbUserBPM.SelectAccountList(ChangeStr.ToArray(), "EnterpriseID", "CreationDate DESC", 1, 0, "EnterpriseID");
//EnterList = XbEnterpriseBPM.SelectEnterpriseList(ChangeStr.ToArray(), "ID", "CreationDate DESC", 1, 0, "ID");
if (Acclist.Count > 0)
{
return Acclist[0].EnterpriseID.ToString();
}
else
{
return Entname;
}
}
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">宽</param>
/// <param name="height">高</param>
public static void ReducedThumbnail(string originalImagePath, string thumbnailPath, int width, int height)
{
Bitmap img = new Bitmap(originalImagePath); //read picture to memory
int h = img.Height;
int w = img.Width;
int ss, os;// source side and objective side
double temp1, temp2;
//compute the picture's proportion
temp1 = (h * 1.0D) / height;
temp2 = (w * 1.0D) / width;
if (temp1 < temp2)
{
ss = w;
os = width;
}
else
{
ss = h;
os = height;
}
double per = (os * 1.0D) / ss;
if (per < 1.0D)
{
h = (int)(h * per);
w = (int)(w * per);
}
// create the thumbnail image
System.Drawing.Image imag2 = img.GetThumbnailImage(w, h,
new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback),
IntPtr.Zero);
Bitmap tempBitmap = new Bitmap(w, h);
System.Drawing.Image tempImg = System.Drawing.Image.FromHbitmap(tempBitmap.GetHbitmap());
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(tempImg);
g.Clear(Color.White);
int x, y;
x = (tempImg.Width - imag2.Width) / 2;
y = (tempImg.Height - imag2.Height) / 2;
g.DrawImage(imag2, x, y, imag2.Width, imag2.Height);
try
{
if (img != null)
img.Dispose();
if (imag2 != null)
imag2.Dispose();
if (tempBitmap != null)
tempBitmap.Dispose();
string fileExtension = System.IO.Path.GetExtension(thumbnailPath).ToLower();
//按原图片类型保存缩略图片,不按原格式图片会出现模糊,锯齿等问题.
switch (fileExtension)
{
case ".gif": tempImg.Save(thumbnailPath, ImageFormat.Gif); break;
case ".jpg": tempImg.Save(thumbnailPath, ImageFormat.Jpeg); break;
case ".jpeg": tempImg.Save(thumbnailPath, ImageFormat.Jpeg); break;
case ".bmp": tempImg.Save(thumbnailPath, ImageFormat.Bmp); break;
case ".png": tempImg.Save(thumbnailPath, ImageFormat.Png); break;
}
}
catch
{
throw new Exception("图片上传失败");
}
finally
{
//释放内存
if (tempImg != null)
tempImg.Dispose();
if (g != null)
g.Dispose();
}
}
#region 压缩图片
/// <summary>
/// 压缩图片
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="encoderPara">使用不同的质量级别保存每个 JPEG 图像。质量级别 0L 对应于最大压缩,而质量级别 100L 对应于最小压缩。</param>
public static void VaryQualityLevel(string originalImagePath, string thumbnailPath, Int64 encoderPara)
{
// Get a bitmap.
Bitmap bmp1 = new Bitmap(@"" + originalImagePath + "");
ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
// Create an Encoder object based on the GUID
// for the Quality parameter category.
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, encoderPara);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"" + thumbnailPath + "", jgpEncoder, myEncoderParameters);
//释放内存
if (bmp1 != null)
bmp1.Dispose();
}
public static ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
public static Int64 GetLevel(Int64 fileLenth)
{
Int64 encoderPara = 0L;
if (fileLenth < 400000)
{
encoderPara = 95L;
}
else if (fileLenth > 400000 && fileLenth <= 600000)
{
encoderPara = 90L;
}
else if (fileLenth > 600000 && fileLenth <= 800000)
{
encoderPara = 80L;
}
else if (fileLenth > 800000 && fileLenth <= 1000000)
{
encoderPara = 65L;
}
else if (fileLenth > 1000000 && fileLenth <= 2000000)
{
encoderPara = 50L;
}
else if (fileLenth > 2000000)
{
encoderPara = 30L;
}
return encoderPara;
}
#endregion
private static bool ThumbnailCallback()
{
return false;
}
#endregion
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Net;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Drawing;
#region 图片处理
protected void Button3_Click(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(Server.MapPath("/Upload"));
FileSystemInfo[] dis = di.GetFileSystemInfos();
if (dis.Length < 1)
{
}
else
{
for (int i = 0; i < dis.Length; i++) {
//修改文件名
string path = Server.MapPath("/Upload/") + dis[i].ToString();
//修改该文件夹下的文件夹名
string logopath = path + "/logo";
DirectoryInfo di3 = new DirectoryInfo(logopath);
if (di3.Exists) {
string logopathnew = path + "/Brand";
try
{
di3.MoveTo(logopathnew);
}
catch { }
}
string Productpath = path + "/产品";
DirectoryInfo di4 = new DirectoryInfo(Productpath);
if (di4.Exists) {
string Productpath4 = path + "/product";
try
{
di4.MoveTo(Productpath4);
}
catch { }
}
//生成产品缩图
Productpath = path + "/Product";
DirectoryInfo di5 = new DirectoryInfo(Productpath);
if (di5.Exists) {
FileSystemInfo[] fis = di5.GetFileSystemInfos();
if (fis.Length > 1) {
for (int j = 0; j < fis.Length; j++) {
if (File.Exists(Productpath + "/" + fis[j].ToString()))
{
ReducedThumbnail(Productpath + "/" + fis[j].ToString(), Productpath + "/S-" + fis[j].ToString(), 115, 104);
}
}
}
}
//修改该文件夹的名字
DirectoryInfo di2 = new DirectoryInfo(path);
if (di2.Exists)
{
string path1 = Server.MapPath("/Upload/") + GetEnterpriseIdByName2(dis[i].ToString());
try
{
di2.MoveTo(path1);
}
catch { }
}
}
}
}
public string GetEnterpriseIdByName2(string Entname) {
//List<XbEnterprise> EnterList = new List<XbEnterprise>();
List<XbAccount> Acclist = new List<XbAccount>();
List<string> ChangeStr = new List<string>();
ChangeStr.Add("LoginID='" + Entname + "'");
Acclist = XbUserBPM.SelectAccountList(ChangeStr.ToArray(), "EnterpriseID", "CreationDate DESC", 1, 0, "EnterpriseID");
//EnterList = XbEnterpriseBPM.SelectEnterpriseList(ChangeStr.ToArray(), "ID", "CreationDate DESC", 1, 0, "ID");
if (Acclist.Count > 0)
{
return Acclist[0].EnterpriseID.ToString();
}
else
{
return Entname;
}
}
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">宽</param>
/// <param name="height">高</param>
public static void ReducedThumbnail(string originalImagePath, string thumbnailPath, int width, int height)
{
Bitmap img = new Bitmap(originalImagePath); //read picture to memory
int h = img.Height;
int w = img.Width;
int ss, os;// source side and objective side
double temp1, temp2;
//compute the picture's proportion
temp1 = (h * 1.0D) / height;
temp2 = (w * 1.0D) / width;
if (temp1 < temp2)
{
ss = w;
os = width;
}
else
{
ss = h;
os = height;
}
double per = (os * 1.0D) / ss;
if (per < 1.0D)
{
h = (int)(h * per);
w = (int)(w * per);
}
// create the thumbnail image
System.Drawing.Image imag2 = img.GetThumbnailImage(w, h,
new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback),
IntPtr.Zero);
Bitmap tempBitmap = new Bitmap(w, h);
System.Drawing.Image tempImg = System.Drawing.Image.FromHbitmap(tempBitmap.GetHbitmap());
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(tempImg);
g.Clear(Color.White);
int x, y;
x = (tempImg.Width - imag2.Width) / 2;
y = (tempImg.Height - imag2.Height) / 2;
g.DrawImage(imag2, x, y, imag2.Width, imag2.Height);
try
{
if (img != null)
img.Dispose();
if (imag2 != null)
imag2.Dispose();
if (tempBitmap != null)
tempBitmap.Dispose();
string fileExtension = System.IO.Path.GetExtension(thumbnailPath).ToLower();
//按原图片类型保存缩略图片,不按原格式图片会出现模糊,锯齿等问题.
switch (fileExtension)
{
case ".gif": tempImg.Save(thumbnailPath, ImageFormat.Gif); break;
case ".jpg": tempImg.Save(thumbnailPath, ImageFormat.Jpeg); break;
case ".jpeg": tempImg.Save(thumbnailPath, ImageFormat.Jpeg); break;
case ".bmp": tempImg.Save(thumbnailPath, ImageFormat.Bmp); break;
case ".png": tempImg.Save(thumbnailPath, ImageFormat.Png); break;
}
}
catch
{
throw new Exception("图片上传失败");
}
finally
{
//释放内存
if (tempImg != null)
tempImg.Dispose();
if (g != null)
g.Dispose();
}
}
#region 压缩图片
/// <summary>
/// 压缩图片
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="encoderPara">使用不同的质量级别保存每个 JPEG 图像。质量级别 0L 对应于最大压缩,而质量级别 100L 对应于最小压缩。</param>
public static void VaryQualityLevel(string originalImagePath, string thumbnailPath, Int64 encoderPara)
{
// Get a bitmap.
Bitmap bmp1 = new Bitmap(@"" + originalImagePath + "");
ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
// Create an Encoder object based on the GUID
// for the Quality parameter category.
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, encoderPara);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"" + thumbnailPath + "", jgpEncoder, myEncoderParameters);
//释放内存
if (bmp1 != null)
bmp1.Dispose();
}
public static ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
public static Int64 GetLevel(Int64 fileLenth)
{
Int64 encoderPara = 0L;
if (fileLenth < 400000)
{
encoderPara = 95L;
}
else if (fileLenth > 400000 && fileLenth <= 600000)
{
encoderPara = 90L;
}
else if (fileLenth > 600000 && fileLenth <= 800000)
{
encoderPara = 80L;
}
else if (fileLenth > 800000 && fileLenth <= 1000000)
{
encoderPara = 65L;
}
else if (fileLenth > 1000000 && fileLenth <= 2000000)
{
encoderPara = 50L;
}
else if (fileLenth > 2000000)
{
encoderPara = 30L;
}
return encoderPara;
}
#endregion
private static bool ThumbnailCallback()
{
return false;
}
#endregion
同事编写的,拿用记录一下,下次要用就很方便了,^0^