linux cpu占用率如何看
530
2022-10-24
本文目录一览:
可以使用base64在线转图片工具: 直接拖动图片到该工具即可自动计算base64编码,望采纳,谢谢!
不用保存成文件。
写一个servlet(假设名字是servletImg),页面的参数就是 id,然后将从DB得到的图像的byte[],通过流输出给页面。
页面还是 img src="servletImg?id=1234" /
这样的逻辑,才行。
如果已经是base64格式的图片,那么可以直接使用使用。
CSS中使用:background-image: url("data:image/png;base64,iVBORw0KGgo=...");
HTML中使用:src="data:image/png;base64,iVBORw0KGgo=..."。
详细的使用和原理可以参考:
需要在php端处理base64字符串里的头部信息
贴一段我正在用的
php($stream是你传上来的base64
//获取扩展名和文件名
if (preg_match('/(?=\/)[^\/]+(?=\;)/',$stream,$pregR)) $streamFileType ='.' .$pregR[0]; //读取扩展名,如果你的程序仅限于画板上来的,那一定是png,这句可以直接streamFileType 赋值png
$streamFileRand = date('YmdHis').rand(1000,9999); //产生一个随机文件名(因为你base64上来肯定没有文件名,这里你可以自己设置一个也行)
$streamFilename = $upPath."/".$streamFileRand .$streamFileType;
//处理base64文本,用正则把第一个base64,之前的部分砍掉
preg_match('/(?=base64,)[\S|\s]+/',$stream,$streamForW);
if (file_put_contents($streamFilename,base64_decode($streamForW[0]))===false) Common::exitWithError("文件写入失败!","");//这是我自己的一个静态类,输出错误信息的,你可以换成你的程序
字符串,假设随机命名,如果你不要随机命名,可以改streamFileRand 的值,$upPath是你上传路径):
输出html 格式,放歌img 标签,1先把base64编码转为图片,再生成world.
/// summary
/// base64编码的文本转为图片
/// /summary
/// param name="txtFilePath"文件相对路径(存到服务器上)/param
/// param name="str"图片字符串/param
private void Base64StringToImage(string txtFilePath, string str)
{
try
{
String inputStr = str;
byte[] arr = Convert.FromBase64String(inputStr);
MemoryStream ms = new MemoryStream(arr);
Bitmap bmp = new Bitmap(ms);
bmp.Save(System.Web.HttpContext.Current.Server.MapPath(txtFilePath) + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
//bmp.Save(txtFileName + ".bmp", ImageFormat.Bmp);
//bmp.Save(txtFileName + ".gif", ImageFormat.Gif);
//bmp.Save(txtFileName + ".png", ImageFormat.Png);
ms.Close();
//imgPhoto.ImageUrl = txtFilePath + ".jpg";
//MessageBox.Show("转换成功!");
}
catch (Exception ex)
{
}
}
public void ExportControl(System.Web.UI.Control source, string DocumentType, string filename)
{
//设置Http的头信息,编码格式
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
if (DocumentType.ToLower() == "excel")
{
//Excel
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".xls", System.Text.Encoding.UTF8));
HttpContext.Current.Response.ContentType = "application/ms-excel";
}
else if (DocumentType.ToLower() == "word")
{
//Word
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".doc", System.Text.Encoding.UTF8));
HttpContext.Current.Response.ContentType = "application/ms-word";
}
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.HeaderEncoding=System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
//关闭控件的视图状态
source.Page.EnableViewState = false;
//初始化HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
source.RenderControl(htmlWriter);
//输出
HttpContext.Current.Response.Write(writer.ToString());
HttpContext.Current.Response.End();
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~