vue加载static文件夹外的图片——以base64方式传图及压缩图片传递
2019-03-31 16:22
148 查看
解决VUE只能加载static文件夹图片的问题,涉及代码有参考他人代码。
图片压缩参考:https://www.cnblogs.com/21dacia/articles/1335495.html
图片转base64参考:https://www.geek-share.com/detail/2711539701.html
1.C#部分代码,实现转化为Base64及压缩等功能
/// <summary> /// 展示图片 /// </summary> /// <param name="filename"></param> /// <param name="ratio">是否压缩</param> /// <returns></returns> public HttpResponseMessage ShowImgInfo([FromUri]string local, [FromUri]string company, [FromUri]string infoYear, [FromUri]string filename, [FromUri]int ratio) { string msg = "error"; if (string.IsNullOrEmpty(filename)) { return new HttpResponseMessage() { Content = new StringContent(JsonConvert.SerializeObject(msg), Encoding.UTF8, "application/json") }; } // 拼接图片文件路径 string path = System.Configuration.ConfigurationManager.AppSettings["file_pic"]; string fullPath = System.IO.Path.Combine(path, string.Format(@"{0}_{3}\{1}\{2}", company, local, filename, infoYear)); byte[] bytes; string imgBase64 = ""; if (System.IO.File.Exists(fullPath)) { if (ratio == 0) { // 返回原图 System.IO.FileStream fs = new System.IO.FileStream(fullPath, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite); bytes = new byte[fs.Length]; System.IO.BinaryReader r = new System.IO.BinaryReader(fs); bytes = r.ReadBytes((int) fs.Length); r.Close(); r = null; fs.Close(); } else { // 压缩图片 System.Drawing.Image img = System.Drawing.Image.FromFile(fullPath); System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat; Bitmap outBmp = new Bitmap(58, 58); Graphics g = Graphics.FromImage(outBmp); // 设置画布的描绘质量 g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(img, new Rectangle(0, 0, 58, 58), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel); g.Dispose(); // 以下代码为保存图片时,设置压缩质量 EncoderParameters encoderParams = new EncoderParameters(); long[] quality = new long[1]; quality[0] = 100; EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); encoderParams.Param[0] = encoderParam; //获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象. ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegICI = null; for (int x = 0; x < arrayICI.Length; x++) { if (arrayICI[x].FormatDescription.Equals("JPEG")) { jpegICI = arrayICI[x]; //设置JPEG编码 break; } } MemoryStream ms = new MemoryStream(); if (jpegICI != null) { outBmp.Save(ms, jpegICI, encoderParams); } else { outBmp.Save(ms, thisFormat); } img.Dispose(); outBmp.Dispose(); bytes = new byte[ms.Length]; ms.Position = 0; ms.Read(bytes, 0, (int)ms.Length); ms.Close(); } imgBase64 = Convert.ToBase64String(bytes);// 转化为base64格式 } msg = string.Format("data:image/gif;base64,{0}",imgBase64); return new HttpResponseMessage() { Content = new StringContent(JsonConvert.SerializeObject(msg), Encoding.UTF8, "application/json") }; }
2.VUE界面加载
this.viewurl = this.formatImgeUrl(this.imgName,0);
formatImgeUrl(filename,ratio) {
var baseurl;
KaTeX parse error: Expected '}', got 'EOF' at end of input: …ax({
url: this.baseurl + “/voc/api/showimginfo”,
type: “get”,
async: false,
data: {
local: this.local,
company: this.company,
infoYear: this.infoYear,
filename: filename,
ratio: ratio
},
success: json => {
if (json) {
if (json !== “error”) {
baseurl = json;
}
}
}
});
return baseurl;
}
相关文章推荐
- 安卓、IOS-web调用摄像头/本地文件夹以base64压缩图片传递给服务器并还原为图片
- vue-lazyload实现图片懒加载的方式
- Android加载SD卡目录,文件夹遍历,图片设置,设置文件对应打开方式等
- Android加载SD卡目录,文件夹遍历,图片设置,设置文件对应打开方式等
- vue上传图片(压缩、base64)
- 图片转成BASE64字符串,然后以JSON的方式传递。当图片大小在1M左右的时候没问题,可以上传。但是当文件大小>1.5M的是否发现图片转BASE64不成功,居然得到的值是NULL
- Android中加载高清大图及图片压缩方式
- 压缩图片加载的简单方式
- Android中加载高清大图及图片压缩方式
- hitTest方法所做的工作,事件传递,图片的两种加载方式的区别,毛玻璃效果,记录一下
- webpack3实战(5)打包一个多页、jQuery、图片转base64、压缩混淆、异步模块加载的项目
- Android加载SD卡目录,文件夹遍历,图片设置,设置文件对应打开方式等
- Android加载SD卡目录,文件夹遍历,图片设置,设置文件对应打开方式等
- 浅谈android中加载高清大图及图片压缩方式(二)
- Android图片bitmap的六种压缩方式
- 使用html5+的plus调起相机拍照,使用canvas压缩图片,转成base64传到后台
- iOS开发 正确选择图片加载方式
- Android三种基本的加载网络图片方式
- Android异步加载图片详解之方式二(2)
- 图片加载方式