using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Net;
using System.IO;
using KT_Product_Show_V3.Models;
using KT_Product_Show_V3.Controllers;
using System.Text.RegularExpressions;
using System.Drawing.Imaging; namespace KT_Product_Show_V3
{
public partial class ImageTool
{
public static string BuildImg(string webUrl, int Size, string directory)
{
Bitmap bmp;
System.Drawing.Image img;
string filename = "";
if (!Directory.Exists(directory))
{
return "";
}
WebRequest WebReq = WebRequest.Create(webUrl);
WebReq.Method = "GET";
img = System.Drawing.Image.FromStream(WebReq.GetResponse().GetResponseStream());
bmp = new Bitmap(Size, Size);
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(, , Size, Size);
gr.DrawImage(img, rectDestination, , , img.Width, img.Height, GraphicsUnit.Pixel);
filename = DateTime.Now.ToFileTime().ToString() + "_" + Size + ".jpeg";
bmp.Save(directory + filename);
Qiniu_API.PutFile(directory + filename, filename);
bmp.Dispose();
img.Dispose();
return filename;
} public static string Base64StringToImage(string strsuffix, string strbase64)
{
string path = "C:\\DescripttionImg\\";
string FileName_withOutstrsuffix = DateTime.Now.ToString("yyMMddhhmmss") + Guid.NewGuid().ToString("N");
string FileName = string.Format(@"{0}.{1}", FileName_withOutstrsuffix, strsuffix);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
try
{
byte[] arr = Convert.FromBase64String(strbase64);
MemoryStream ms = new MemoryStream(arr);
Bitmap bmp = new Bitmap(ms);
bmp.Save(path + FileName);
ms.Close(); Qiniu_API.PutFile(path + FileName, FileName); }
catch (Exception ex)
{ }
finally
{ }
return string.Format("http://{0}/{1}", Qiniu_API.Domain, FileName);
} public static string ImgToBase64String(string Imagefilename)
{
try
{
Bitmap bmp = new Bitmap(Imagefilename); MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] arr = new byte[ms.Length];
ms.Position = ;
ms.Read(arr, , (int)ms.Length);
ms.Close();
return Convert.ToBase64String(arr);
}
catch (Exception ex)
{
return null;
}
} public static string DescripttionDecode(string str)
{
string pattern = @"data:image/[a-z]+;base64,([a-z0-9-A-z]|[\+]|[/\\]|[=])*";
string pattern_suffix = @"data:image/[a-z]+;base64,";
RegexOptions regexOptions = RegexOptions.None;
Regex regex = new Regex(pattern, regexOptions);
Regex regex_suffix = new Regex(pattern_suffix, regexOptions);
foreach (Match match in regex.Matches(str))
{
if (match.Success)
{
string path = "";
string Temp = regex_suffix.Match(match.Value).Value;
string suffix = Temp.Replace("data:image/", "").Replace(";base64,", "");
string base64 = match.Value.Replace(Temp, "");
path = Base64StringToImage(suffix, base64);
str = str.Replace(match.Value, path); }
} return str;
} public static string DescripttionDecodeCutMaxWidth(string str, int MaxWidth)
{
string pattern = @"data:image/[a-z]+;base64,([a-z0-9-A-z]|[\+]|[/\\]|[=])*";
string pattern_suffix = @"data:image/[a-z]+;base64,";
RegexOptions regexOptions = RegexOptions.None;
Regex regex = new Regex(pattern, regexOptions);
Regex regex_suffix = new Regex(pattern_suffix, regexOptions);
foreach (Match match in regex.Matches(str))
{
if (match.Success)
{
string path = "";
string Temp = regex_suffix.Match(match.Value).Value;
string suffix = Temp.Replace("data:image/", "").Replace(";base64,", "");
string base64 = match.Value.Replace(Temp, "");
path = Base64StringToImageCutMaxWidth(suffix, base64,MaxWidth);
str = str.Replace(match.Value, path); }
} return str;
}
public static string Base64StringToImageCutMaxWidth(string strsuffix, string strbase64, int MaxWidth)
{
string path = "C:\\DescripttionImg\\";
string FileName_withOutstrsuffix = DateTime.Now.ToString("yyMMddhhmmss") + Guid.NewGuid().ToString("N");
string FileName = string.Format(@"{0}.{1}", FileName_withOutstrsuffix, strsuffix);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
try
{
byte[] arr = Convert.FromBase64String(strbase64);
MemoryStream ms = new MemoryStream(arr);
Bitmap bmp = new Bitmap(ms);
if (bmp.Width > MaxWidth)
{
bmp = KiResizeImage(bmp, MaxWidth, MaxWidth * bmp.Height / bmp.Width);
} // bmp.Width = MaxWidth;
// bmp.Height
bmp.Save(path + FileName);
ms.Close(); Qiniu_API.PutFile(path + FileName, FileName); }
catch (Exception ex)
{ }
finally
{ }
return string.Format("http://{0}/{1}", Qiniu_API.Domain, FileName);
}
public static Bitmap KiResizeImage(Bitmap bmp, int newW, int newH)
{
try
{
Bitmap b = new Bitmap(newW, newH);
Graphics g = Graphics.FromImage(b); // 插值算法的质量
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.DrawImage(bmp, new Rectangle(, , newW, newH), new Rectangle(, , bmp.Width, bmp.Height), GraphicsUnit.Pixel);
g.Dispose(); return b;
}
catch
{
return null;
}
} }
}

先上传到本地 然后到 七牛云 存储,最后 ,在 bbs 上显示 图片 直接 来源于七牛,本地也保存了一份,后续扩展到一键切换到本地读取功能。还可以使用负载均衡。

上面这个  图片操作类 还提供了 头像裁剪 之后 的 上传 大、小 图片 的方法。

[HttpPost]
[ValidateInput(false)]
public void AddDescripttion()
{
if (Request.Form["Descripttion"] == null)
{
Response.Write();
Response.End(); }
string str = Request.Form["Descripttion"].ToString();
Discuz discuz = new Discuz();
discuz.CreateTime = DateTime.Now;
discuz.Descripttion = ImageTool.DescripttionDecode(str);
discuz.Floor = conn.Database.SqlQuery<int>("select isnull( max(Floor),0) from dbo.Discuz").FirstOrDefault() + ;
discuz.ForumId = ;
discuz.IsFather = true;
discuz.Isgood = ;
discuz.IsPass = ;
discuz.UserId = ;
conn.Discuz.Add(discuz);
conn.SaveChanges();
Response.Write();
Response.End();
}

MVC  bbs发表帖子 的调用 action , [ValidateInput(false)]  结合 网站 web.config 文件 中的  <system.webServer> 下的<validation validateIntegratedModeConfiguration="false" />  一起用 ,才能解决

从客户端 中检测到有潜在危险的 Request.Form 值  报错的问题。

还介绍一个好的  Regex 工具

下载之后 我找了 半天 ,原来 在 这里啊!

img  的 src 属性 可以 是 data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA

AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO

9TXL0Y4OHwAAAABJRU5ErkJggg==" >  这种不支持图片缓存。

bootstrap-wysiwyg 结合 base64 解码 .net bbs 图片操作类的更多相关文章

  1. bootstrap-wysiwyg 结合 base64 解码 .net bbs 图片操作类 (二) 图片裁剪

    图片裁剪参见: http://deepliquid.com/projects/Jcrop/demos.php?demo=thumbnail        一个js插件 http://www.mikes ...

  2. bootstrap-wysiwyg 结合 base64 解码 .net bbs 图片操作类 (三) 图片裁剪

    官方的例子 是 长方形的. 我这里 用于 正方形的头像 所以  做如下  修改 #preview-pane .preview-container { width: 73px; height: 73px ...

  3. C#——图片操作类简单封装

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...

  4. php 图片操作类,支持生成缩略图,添加水印,上传缩略图

    <?php class Image {     //类开始     public $originimage = ""; //源图片文件地址     public $image ...

  5. C#获取CPU与网卡硬盘序列号及Base64和DES加密解密操作类

    public class RegisterHelp { /// <summary> /// CPU /// </summary> /// <returns>< ...

  6. 微信小程序之图片base64解码

    不知道大家在做微信小程序的时候遇到base64解码的问题,我之前在做微信小程序的时候遇到base64解析图片一直有问题,所以在这里把遇到的问题和解决方案在这里记录一下: 在平时的项目中我们是直接用ba ...

  7. 将图片文件转化为字节数组字符串,并对其进行Base64编码处理,以及对字节数组字符串进行Base64解码并生成图片

    实际开发中涉及图片上传并且量比较大的时候一般处理方式有三种 1.直接保存到项目中 最老土直接方法,也是最不适用的方法,量大对后期部署很不方便 2.直接保存到指定路径的服务器上.需要时候在获取,这种方式 ...

  8. Base64字符保存图片,图片转换成Base64字符编码

    //文件转换成Base64编码 public static String getFileBase64Str(String filePath) throws IOException { String f ...

  9. file上传图片,base64转换、压缩图片、预览图片、将图片旋转到正确的角度

    /** * 将base64转换为文件对象 * (即用文件上传输入框上传文件得到的对象) * @param {String} base64 base64字符串 */ function convertBa ...

随机推荐

  1. C指针决心 ------ 指针的概念和元素

     本文是自己学习所做笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020 指针在C语言中的地位,不用多说. 指针的概念 指针是一个特殊的变量,它里面存储 ...

  2. jQuery 完成ajax传jsonObject数据,并在后台处理

    效果图: 1.js文件封装的几个js工具 <span style="font-family:KaiTi_GB2312;font-size:18px;">//兼容ie i ...

  3. 【Android进阶】ZXing android 错误(Could not find class 'com.google.zxing.ResultPoint)

    解决方法: 1.右键工程Build path, java build path,选择libraries 在右边的按钮中点击"Add Library" 选择"User li ...

  4. FZU1669 Right-angled Triangle【毕达哥拉斯三元组】

    主题链接: pid=1669">http://acm.fzu.edu.cn/problem.php?pid=1669 题目大意: 求满足以a.b为直角边,c为斜边,而且满足a + b ...

  5. dojo在错误隐藏表行

    1.错误叙述性说明 TypeError:role._by_idx[e.rowIndex].hide is not a function           (54 out of range 3) 2. ...

  6. Java String类的比较运算

    面试题:(多选)以下返回true的有() A. "beijing" == "beijing" B. "beijing".equals(new ...

  7. poj 1699 Best Sequence(AC自己主动机+如压力DP)

    id=1699" target="_blank" style="">题目链接:poj 1699 Best Sequence 题目大意:给定N个D ...

  8. SUPPORTDIR引用的文件的加入

    上一篇转载了SUPPORTDIR的理论解释,如今截图说明实际操作: 选择依赖的语言,在右面的files框里右键点击,选择Insert Files插入文件,编译.由于安装得时候,压缩包会解压成一个暂时文 ...

  9. 命令模式 & 策略模式 & 模板方法

    一.策略模式 策略模式:封装易变化的算法,可互相替换. GoF<设计模式>中说道:定义一系列算法,把它们一个个封装起来,并且使它们可以相互替换.该模式使得算法可独立于它们的客户变化. 比如 ...

  10. python基础课程_学习笔记13:标准库:有些收藏夹——sys

    标准库:有些收藏夹 sys sys这个模块可以让你访问和python解释器联系紧密的变量和函数. sys模块中一些重要的函数和变量 函数/变量 描写叙述 argv 命令行參数,包含脚本名称 exit( ...