本文整理匯總了C#中System.Drawing.Imaging.ImageFormat.ToFileExtension方法的典型用法代碼示例。如果您正苦於以下問題:C# ImageFormat.ToFileExtension方法的具體用法?C# ImageFormat.ToFileExtension怎麽用?C# ImageFormat.ToFileExtension使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Drawing.Imaging.ImageFormat
的用法示例。
在下文中一共展示了ImageFormat.ToFileExtension方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: SaveLocal
// todo 將組CIFILE抽出來
/// <summary>
/// 圖片儲存
/// </summary>
/// <param name="image">The image.</param>
/// <param name="imageFormat">The image format.</param>
/// <param name="folder">欲存放資料夾名稱</param>
/// <returns>CiResult FileUploadViewModel</returns>
protected internal static CiImageFile SaveLocal(Image image, ImageFormat imageFormat, string folder = "Temps", string fileName = "")
{
if (image == null)
{
throw new NullReferenceException("圖片為空!");
}
if (string.IsNullOrWhiteSpace(fileName))
{
fileName = GetNewFileName();
}
CiImageFile model = new CiImageFile()
{
Extension = imageFormat.ToFileExtension(),
NewName = fileName,
Folder = folder,
VirtualPath = Path.Combine(RootPath, folder),
Format = imageFormat
};
model.FullPath =
HttpContext.Current.Server.MapPath(Path.Combine(model.VirtualPath, model.NewName + model.Extension));
// 若檔名重複則自動重新取得編號
while (File.Exists(model.FullPath))
{
model.NewName = GetNewFileName();
model.FullPath =
HttpContext.Current.Server.MapPath(Path.Combine(model.VirtualPath, model.NewName + model.Extension));
}
// 檢查資料夾是否存在,若不存在則自動建立
if (!Directory.Exists(Path.GetDirectoryName(model.FullPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(model.FullPath));
}
// 存檔
image.Save(model.FullPath, imageFormat);
model.VirtualPath = Path.Combine(model.VirtualPath, model.NewName + model.Extension);
return model;
}