/// <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 生成图片,下载文件(图片不存在本地,在网上下载)的更多相关文章

  1. java 从服务器下载文件并保存到本地

    昨天在做一个项目时,用到了从服务器上下载文件并保存到本地的知识,以前也没有接触过,昨天搞了一天,这个小功能实现了,下面就简单的说一下实现过程: 1.基础知识          当我们想要下载网站上的某 ...

  2. WPF带cookie get/post请求网页,下载文件,图片,可保持会话状态

    直接写成啦一个MyNet.cs类方便使用 get/post方法请求 //get请求 MyNet.SendRequest("http://www.baidu.com"); //pos ...

  3. android:http下载文件并保存到本地或SD卡

    想把文件保存到SD卡中,一定要知道SD卡的路径,获取SD卡路径: Environment.getExternalStorageDirectory() 另外,在保存之前要判断SD卡是否已经安装好,并且可 ...

  4. android中使用Http下载文件并保存到本地SD卡

    1.AndroidMainfest.xml中设置权限 <uses-permission android:name="android.permission.INTERNET"& ...

  5. Android HTTP下载文件并保存到本地或SD卡

    想把文件保存到SD卡中,一定要知道SD卡的路径,获取SD卡路径: Environment.getExternalStorageDirectory() 另外,在保存之前要判断SD卡是否已经安装好,并且可 ...

  6. axios通过post请求下载文件/图片

    我们平常下载文件一般都是通过get请求直接访问进行下载, 但是当有特殊情况如权限控制之类的会要求我们通过post请求进行下载,这时就不一样了, 具体方法是通过协调后端,约定返回的文件流,请求的resp ...

  7. centos7用rpm安装mysql5.7【初始用yum安装发现下载非常慢,就考虑本地用迅雷下载rpm方式安装】

    1.下载 4个rpm包 mysql-community-client-5.7.26-1.el7.x86_64.rpmmysql-community-common-5.7.26-1.el7.x86_64 ...

  8. ASP.NET下载远程图片保存到本地的方法、保存抓取远程图片

    以下介绍两种方法:1.利用WebRequest,WebResponse 类 WebRequest wreq=WebRequest.Create("http://www.xueit.com/e ...

  9. 用ASP.NET实现下载远程图片保存到本地的方法 保存抓取远程图片的方法

    以下介绍两种方法:1.利用WebRequest,WebResponse 类WebRequest wreq=WebRequest.Create("http://files.jb51.net/f ...

  10. linux上创建ftp服务器下载文件///使用AWS服务器作为代理,下载sbt相关的包

    最近觉得自己下载有些jar的速度太慢了,就在aws上下好了,然后转到我电脑上来,在aws上开了ftp服务器.结果就倒腾了一上午,作个记录,以便后面查看. 1.安装vsftpd yum -y insta ...

随机推荐

  1. 给swift程序猿留下深刻印象的10个Swift代码

    通过使用单行代码完成同样的 10 个练习,我们来看看 Swift 和其他语言之间的较量. 将数组中每个元素的值乘以 2 使用map来实现 var arr = [1,2,3,4]; var newArr ...

  2. React-Native 给客户端来个「同音词模糊搜索」

    APP上线一段时间有用户反应说不方便找东西,其实APP的数据不大也就三四百条而以,但受限于手机端展示区域太小.信息大爆炸,大家也基本上不会去记具体的名称都是根据模糊的印象进行搜索而且现在大家基本都用拼 ...

  3. OpenGL的简单研究-开端

    一直想要学习的但是没有学习的东西,大学一直在等待这个时间,终于可以闲下来研究一下这个部分的内容了. 计算机图形学,让计算机处理各种图像的东西,里面也存在很多算法和数学知识,很值得研究的一个领域,之前一 ...

  4. python注释

    初学python 习得注释方法 如下: #我是注释 print("hello") ''' 我是 多行 注释! ''' 其实就是#号和三个单引号(也可以双引号)啦 看下面:这注释其实 ...

  5. js隐藏或显示某区域

    隐藏: document.getElementById(“keleyi”).style.display = “none”; 显示: document.getElementById(“keleyi”). ...

  6. DataGridView修改HeaderText

    dataGridView_htList为一个 DataGridView(ht为HoverTree的缩写)方法一:dataGridView_htList.Columns["HtAddTime& ...

  7. 介绍开源的.net通信框架NetworkComms框架 源码分析(九) IPConnection

    原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架  作者是英国人  以前是收费的 目前作者已经开源  许可是 ...

  8. marquee标签滚动效果

    <marquee></marquee>标签,默认从最右侧往左滚动: direction:设置滚动的方向: height:设置标签高度, width:设置标签宽度: behavi ...

  9. spring 使用redis集群配置

    上面两篇介绍了redis集群的配置合一些基本的概念,所以接下来当然是要在项目中使用咯,redis的java支持已经做的非常好了,所以我们来试着使用这些api来进行redis的操作,首先我们需要操作re ...

  10. 你知道url中的特殊符号含义么

    1.# #代表网页中的一个位置.其右面的字符,就是该位置的标识符.比如,http://www.example.com/index.html#print就代表网页index.html的print位置.浏 ...