📄 imageupload.cs
字号:
{
get { return _Height; }
}
/// <summary>
/// 设置缩略图的宽度
/// </summary>
public int sWidth
{
get { return _sWidth; }
set { _sWidth = value; }
}
/// <summary>
/// 设置缩略图的高度
/// </summary>
public int sHeight
{
get { return _sHeight; }
set { _sHeight = value; }
}
/// <summary>
/// 是否生成缩略图
/// </summary>
public bool IsCreateImg
{
get { return _IsCreateImg; }
set { _IsCreateImg = value; }
}
/// <summary>
/// 是否加水印
/// </summary>
public bool IsDraw
{
get { return _IsDraw; }
set { _IsDraw = value; }
}
/// <summary>
/// 设置加水印的方式
/// 0:文字水印模式
/// 1:图片水印模式
/// 2:不加
/// </summary>
public int DrawStyle
{
get { return _DrawStyle; }
set { _DrawStyle = value; }
}
/// <summary>
/// 绘制文本的X坐标(左上角)
/// </summary>
public int DrawString_x
{
get { return _DrawString_x; }
set { _DrawString_x = value; }
}
/// <summary>
/// 绘制文本的Y坐标(左上角)
/// </summary>
public int DrawString_y
{
get { return _DrawString_y; }
set { _DrawString_y = value; }
}
/// <summary>
/// 设置文字水印内容
/// </summary>
public string AddText
{
get { return _AddText; }
set { _AddText = value; }
}
/// <summary>
/// 设置文字水印字体
/// </summary>
public string Font
{
get { return _Font; }
set { _Font = value; }
}
/// <summary>
/// 设置文字水印字的大小
/// </summary>
public int FontSize
{
get { return _FontSize; }
set { _FontSize = value; }
}
/// <summary>
/// 文件大小
/// </summary>
public int FileSize
{
get { return _FileSize; }
set { _FileSize = value; }
}
/// <summary>
/// 图片水印模式下的覆盖图片的实际地址
/// </summary>
public string CopyIamgePath
{
set { _CopyIamgePath = System.Web.HttpContext.Current.Server.MapPath(value); }
}
#endregion
#region 私有方法
/// <summary>
/// 获取文件的后缀名
/// </summary>
private string GetExt(string path)
{
return Path.GetExtension(path);
}
/// <summary>
/// 获取输出文件的文件名
/// </summary>
private string FileName(string Ext)
{
if (_SaveType == 0 || _InFileName.Trim() == "")
return DateTime.Now.ToString("yyyyMMddHHmmssfff") + Ext;
else
return _InFileName;
}
/// <summary>
/// 检查上传的文件的类型,是否允许上传。
/// </summary>
private bool IsUpload(string Ext)
{
Ext = Ext.Replace(".", "");
bool b = false;
string[] arrFileType = _FileType.Split(';');
foreach (string str in arrFileType)
{
if (str.ToLower() == Ext.ToLower())
{
b = true;
break;
}
}
return b;
}
#endregion
#region 上传图片
public void Upload()
{
HttpPostedFile hpFile = _FormFile.PostedFile;
if (hpFile == null || hpFile.FileName.Trim() == "")
{
_Error = 1;
return;
}
string Ext = GetExt(hpFile.FileName);
if (!IsUpload(Ext))
{
_Error = 2;
return;
}
int iLen = hpFile.ContentLength;
if (iLen > _MaxSize)
{
_Error = 3;
return;
}
try
{
if (!Directory.Exists(_SavePath)) Directory.CreateDirectory(_SavePath);
byte[] bData = new byte[iLen];
hpFile.InputStream.Read(bData, 0, iLen);
string FName;
FName = FileName(Ext);
string TempFile = "";
if (_IsDraw)
{
TempFile = FName.Split('.').GetValue(0).ToString() + "_temp." + FName.Split('.').GetValue(1).ToString();
}
else
{
TempFile = FName;
}
FileStream newFile = new FileStream(_SavePath + TempFile, FileMode.Create);
newFile.Write(bData, 0, bData.Length);
newFile.Flush();
int _FileSizeTemp = hpFile.ContentLength;
string ImageFilePath = _SavePath + FName;
if (_IsDraw)
{
if (_DrawStyle == 0)
{
System.Drawing.Image Img1 = System.Drawing.Image.FromStream(newFile);
Graphics g = Graphics.FromImage(Img1);
g.DrawImage(Img1, 100, 100, Img1.Width, Img1.Height);
Font f = new Font(_Font, _FontSize);
Brush b = new SolidBrush(Color.Red);
string addtext = _AddText;
g.DrawString(addtext, f, b, _DrawString_x, _DrawString_y);
g.Dispose();
Img1.Save(ImageFilePath);
Img1.Dispose();
}
else
{
System.Drawing.Image image = System.Drawing.Image.FromStream(newFile);
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(_CopyIamgePath);
Graphics g = Graphics.FromImage(image);
g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width - 5, image.Height - copyImage.Height - 5, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
g.Dispose();
image.Save(ImageFilePath);
image.Dispose();
}
}
//获取图片的高度和宽度
System.Drawing.Image Img = System.Drawing.Image.FromStream(newFile);
_Width = Img.Width;
_Height = Img.Height;
//生成缩略图部分
if (_IsCreateImg)
{
#region 缩略图大小只设置了最大范围,并不是实际大小
float realbili = (float)_Width / (float)_Height;
float wishbili = (float)_sWidth / (float)_sHeight;
//实际图比缩略图最大尺寸更宽矮,以宽为准
if (realbili > wishbili)
{
_sHeight = (int)((float)_sWidth / realbili);
}
//实际图比缩略图最大尺寸更高长,以高为准
else
{
_sWidth = (int)((float)_sHeight * realbili);
}
#endregion
this.OutThumbFileName = FName.Split('.').GetValue(0).ToString() + "_s." + FName.Split('.').GetValue(1).ToString();
string ImgFilePath = _SavePath + this.OutThumbFileName;
System.Drawing.Image newImg = Img.GetThumbnailImage(_sWidth, _sHeight, null, System.IntPtr.Zero);
newImg.Save(ImgFilePath);
newImg.Dispose();
_Iss = true;
}
if (_IsDraw)
{
if (File.Exists(_SavePath + FName.Split('.').GetValue(0).ToString() + "_temp." + FName.Split('.').GetValue(1).ToString()))
{
newFile.Dispose();
File.Delete(_SavePath + FName.Split('.').GetValue(0).ToString() + "_temp." + FName.Split('.').GetValue(1).ToString());
}
}
newFile.Close();
newFile.Dispose();
_OutFileName = FName;
_FileSize = _FileSizeTemp;
_Error = 0;
return;
}
catch (Exception ex)
{
_Error = 4;
return;
}
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -