MVC 產生圖片,下載檔案(圖片不存在本地,在網上下載)

來源:互聯網
上載者:User

標籤:style   http   color   io   os   ar   for   檔案   sp   

/// <summary>
/// 產生圖片
/// </summary>
/// <param name="collection"></param>
/// <returns></returns>
public FileStreamResult GenerateImage(FormCollection collection)
{
var shopID = ShopCaches.GetShopInfo();
var headImgInfo = GetResourcePath(shopID.ShopID, collection["DaYinPersonnelID"], "HeadFront");
var imageInfo = GetResourcePath(shopID.ShopID, collection["inputGongHao"], "PersonNoBarcode");//轉成圖片類型
var filePath = Server.MapPath(string.Format("~/UI/Images/WorkCardPicture/{0}", shopID.ShopID));
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
const int WIDTH = 350, HEIGHT = 230;
var font12B = new Font(FontFamily.GenericSerif, 12.0f, FontStyle.Regular);
var font20B = new Font(FontFamily.GenericSerif, 15.0f, FontStyle.Bold);

using (var bitmap = new Bitmap(WIDTH, HEIGHT))
{
using (Graphics garphics = Graphics.FromImage(bitmap))
{
garphics.Clear(Color.White);
garphics.DrawString(shopID.ShopName, font20B, Brushes.Black, 126, 10);
if (headImgInfo != null)
{
garphics.DrawImage(headImgInfo, 38, 57, 100, 110); //照片
}
garphics.DrawString("工號:" + collection["inputGongHao"], font12B, Brushes.Black, 197, 53);
garphics.DrawString("姓名:" + collection["inputXingMing"], font12B, Brushes.Black, 197, 78);
garphics.DrawString("職位:" + collection["inputZhiWei"], font12B, Brushes.Black, 197, 103);
garphics.DrawImage(imageInfo, new Point(197, 151)); //條碼

bitmap.Save(filePath + "/" + collection["inputGongHao"] + ".jpg");
imageInfo.Dispose();//釋放條碼的所有資源
garphics.Dispose();//釋放資源
bitmap.Dispose();//釋放資源
}
}
var path = filePath + "\\" + collection["inputGongHao"] + ".jpg";
return File(new FileStream(path, FileMode.Open), "image/jpeg", Server.UrlEncode(collection["inputGongHao"] + ".jpg"));
//return Json("");
}

/// <summary>
/// 產生圖片:擷取相應圖片的路徑(先取本地;本地沒有取資原始伺服器)
/// </summary>
/// <param name="shopID">店鋪ID</param>
/// <param name="fileName">要取的圖片的名字</param>
/// <param name="folderName">檔案夾的名字</param>
/// <returns>圖片路徑</returns>
private Image GetResourcePath(Guid shopID, string fileName, string folderName)
{
Image headImgInfo;
const string URL = "~/UI/Images";
string httpurl = WebConfig.ResourceServerInformation;
//判斷本地是否有
var path = Server.MapPath(string.Format("{0}/{1}/{2}/{3}", URL, folderName, shopID, fileName + ".jpg"));
var httpPath = string.Format("{0}/{1}/{2}/{3}", httpurl, folderName, shopID, fileName + ".jpg");
if (System.IO.File.Exists(path))
{
headImgInfo = Image.FromFile(path);
}
else
{
var filePath = Server.MapPath(string.Format("{0}/{1}/{2}", URL, folderName, shopID));
//本地沒有,先把圖片下載下來在轉換
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
string diZhi = Server.MapPath(string.Format("{0}/{1}/{2}/{3}", URL, folderName, shopID, fileName + ".jpg"));
var webClient = new WebClient();
try
{
webClient.DownloadFile(httpPath, diZhi);//下載網路上的圖片,儲存在本地
headImgInfo = Image.FromFile(diZhi);
}
catch
{
headImgInfo = null;
}
}
return headImgInfo;
}

MVC 產生圖片,下載檔案(圖片不存在本地,在網上下載)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.