如何从canvas获取base64字符串 [英] how to get base64 string from canvas

查看:751
本文介绍了如何从canvas获取base64字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 Stream stream =  new  MemoryStream(pict6); 

// 秒表秒表=新秒表();
// stopwatch.Start();
string path = context.Server.MapPath( 〜/ Image / watermark_with_whiteoutline_Stroke2with35%_1200X950.png);

Bitmap _watermarkImage = new 位图(路径); // 水印图片
位图_baseImage = new 位图(流);
位图bmpToSave = 位图( 1200 950 );
使用(Graphics g = Graphics.FromImage(bmpToSave))
{
g.DrawImage(_baseImage, 0 0 1200 950 );
g.Save();
g.Dispose();
}

// 绘制水印偏移距离左侧10个像素和10个来自基本图像顶部的像素。
Graphics canvas = Graphics.FromImage(bmpToSave);
canvas.DrawImage(_watermarkImage, new Point( 50 50 ));
canvas.Save();

bmpToSave.Save(context.Response.OutputStream,ImageFormat.Jpeg);


// 清理

bmpToSave.Dispose();
canvas.Dispose();
_baseImage.Dispose();





以上代码在webform中完美运行..但是我希望在返回后作为base64string返回完成水印..我想需要画布到字节。如果我得到字节然后我可以使用Convert.ToBase64String(byte);



但是如何从画布获取字节?

解决方案

问题和你的问题不明确。使用base64表示你的图像(或其他任何东西)的目的也不清楚。



无论如何,你将图像保存到内存流或临时文件,然后使用 Convert.ToBase64String 方法之一获取所有内存内容:

https://msdn.microsoft.com/en-us/library/system.convert.tobase64string%28v=vs.110 %29.aspx [ ^ ]。



参见:

https://msdn.microsoft.com/en-us/library/system。 drawing.image.save%28v = vs.110%29.aspx [ ^ ],

https://msdn.microsoft。 com / en-us / library / system.io.memorystream%28v = vs.110%29.aspx [ ^ ]。



-SA

Stream stream = new MemoryStream(pict6);

// Stopwatch stopwatch = new Stopwatch();
// stopwatch.Start();
string path = context.Server.MapPath("~/Image/watermark_with_whiteoutline_Stroke2with35%_1200X950.png");

Bitmap _watermarkImage = new Bitmap(path); // watermark image
Bitmap _baseImage = new Bitmap(stream);
Bitmap bmpToSave = new Bitmap(1200, 950);
using (Graphics g = Graphics.FromImage(bmpToSave))
{
g.DrawImage(_baseImage, 0, 0, 1200, 950);
g.Save();
g.Dispose();
}

//Draw the watermark offset 10 pixels from the left and 10 pixels from the top of the base image.
Graphics canvas = Graphics.FromImage(bmpToSave);
canvas.DrawImage(_watermarkImage, new Point(50, 50));
canvas.Save();

bmpToSave.Save(context.Response.OutputStream, ImageFormat.Jpeg);


//clean up

bmpToSave.Dispose();
canvas.Dispose();
_baseImage.Dispose();



The above code works perfectly in webform.. But i want to return as base64string after done watermark.. i guess need to get canvas to byte. if i get byte then i can use Convert.ToBase64String(byte);

But how to get byte from canvas?

解决方案

The question and your problem is not clear. And the purpose of using base64 representation of your image (or anything else) is also not clear.

Anyway, you save an image to a memory stream or temporary file, then take all the memory content using one of Convert.ToBase64String methods:
https://msdn.microsoft.com/en-us/library/system.convert.tobase64string%28v=vs.110%29.aspx[^].

See also:
https://msdn.microsoft.com/en-us/library/system.drawing.image.save%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.io.memorystream%28v=vs.110%29.aspx[^].

—SA


这篇关于如何从canvas获取base64字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆