/// <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,28,57,80,100); //照片
}
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");
}
}
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 = null;
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 if (httpPath!="")
{
//本地没有,先把图片下载下来在转换
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 生成图片,下载文件的更多相关文章

  1. ASP.NET MVC Ajax下载文件(使用NPOI向现有的excel模板文件里面添加数据)

    View Html.DevExpress().Button(DevExpressButtonHelper.AddButton(ViewBag.Form, "Export", &qu ...

  2. spring mvc 避免IE执行AJAX时,返回JSON出现下载文件

    <!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter" c ...

  3. MVC下载文件方式

    MVC下载文件方式 http://www.cnblogs.com/liang--liang/archive/2012/10/20/2732745.html 方式一: public FileStream ...

  4. mvc下载文件

    MVC下载文件方式 方式一: public FileStreamResult DownFile(string filePath, string fileName)  {       string ab ...

  5. spring MVC 下载文件(转)

    springle MVC中如何下载文件呢? 比struts2 下载文件简单得多 先看例子: @ResponseBody @RequestMapping(value = "/download& ...

  6. Asp.net mvc 下载文件

    前言 最近有需求需要下载文件,可能是image的图片,也可能是pdf报告,也可能是微软的word或者excel文件. 这里就整理了asp.net mvc 和asp.net webapi 下载的方法 A ...

  7. Spring MVC之ResposeEntity下载文件

    Spring Mvc中用ResponseEntity方式下载文件如下: @RequestMapping("/download") public ResponseEntity< ...

  8. MVC下载文件方式 包括网络地址文件

    MVC下载文件方式 方式一: public FileStreamResult DownFile(string filePath, string fileName){      string absol ...

  9. 【第十三篇】mvc下载文件,包括配置xml保护服务端文件不被外链直接访问

    这里先说下载文件 <a style="color:black; margin-right:3px;" onclick="dowAtt(' + index + ')& ...

  10. 下载文件时-修改文件名字 Redis在Windows中安装方法 SVN安装和使用(简单版) WinForm-SQL查询避免UI卡死 Asp.Net MVC Https设置

    下载文件时-修改文件名字   1后台代码 /// <summary> /// 文件下载2 /// </summary> /// <param name="Fil ...

随机推荐

  1. 音频文件解析(二):WAV格式文件波形绘制

    解析WAV头部信息后,接下来就可以根据相关参数和DATA块数据绘制波形. 1.重新编码(转换为8bits,单声道数据) Public Function GetFormatData(ByVal pDat ...

  2. MVC过滤器的用法

    APS.NET MVC中的每一个请求,都会分配给相应的控制器和对应的行为方法去处理,而在这些处理的前前后后如果想再加一些额外的逻辑处理.这时候就用到了过滤器. 在Asp.netMvc中当你有以下及类似 ...

  3. C#骏鹏自动售货机接口

    MachineJP类: 第1部分:串口初始化,串口数据读写 using System; using System.Collections.Generic; using System.IO.Ports; ...

  4. 浅谈Oracle中物理结构(数据文件等。。。)与逻辑结构(表空间等。。。。。)

    初始Oracle时很难理解其中的物理结构和逻辑结构,不明白内存中和硬盘中文件的区别和联系,我也是初学Oracle,这里就简单的谈谈我我看法. 首先,你需要明白的一点是:数据库的物理结构是由数据库的操作 ...

  5. 第 15 章 CSS 文本样式[上]

    学习要点: 1.字体总汇 2.字体设置 3.Web 字体 主讲教师:李炎恢 本章主要探讨 HTML5 中 CSS 文本样式,通过文本样式的设置,更改字体的大小.样式以及文本的方位. 一.字体总汇 本节 ...

  6. Eclipse保存文件时自动格式化代码

    实现效果:Ctrl+S会自动格式化并保存代码. 应用上图所示效果之后,在每次对Eclipse保存的时候都会实现自动格式化代码. 1. Fomated All lines,格式化该文件的所有代码:还是 ...

  7. No.018:4Sum

    问题: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  8. extern的用法

    extern作为外部函数声明的用法: 1. 可以扩展函数的应用范围: 107.h #ifndef _107H_ #def _107H_ extern void func(); #endif 107.c ...

  9. (2)RGB-D SLAM系列- 工具篇(依赖库及编译)

    做了个SLAM的小视频,有兴趣的朋友可以看下 https://youtu.be/z5wDzMZF10Q 1)Library depended 一个完整的SLAM系统包括,数据流获取,数据读取,特征提取 ...

  10. 12款响应式的 jQuery 旋转木马(传送带)插件

    在企业网站,作品集网站,电子商务网站或任何其他类型的网站内容显示图片可以使用 jQuery 旋转木马(传送带)插件来实现. jQuery 旋转木马插件允许开发人员以水平或垂直的方式显示内容,视频和图像 ...