C# 将inkpresenter签名转换为Base64字符串
我的要求是需要用InkPresenter捕获用户签名。。。 然后需要将inkpresenter数据转换为base64字符串 我在xaml中的InkpresenterC# 将inkpresenter签名转换为Base64字符串,c#,.net,xaml,base64,C#,.net,Xaml,Base64,我的要求是需要用InkPresenter捕获用户签名。。。 然后需要将inkpresenter数据转换为base64字符串 我在xaml中的Inkpresenter <InkPresenter x:Name="inkSignature" Height="428" Width="450" MouseLeftButtonDown="MyIP_MouseLeftButtonDown" LostMouseCapture="MyIP_
<InkPresenter x:Name="inkSignature" Height="428" Width="450"
MouseLeftButtonDown="MyIP_MouseLeftButtonDown"
LostMouseCapture="MyIP_LostMouseCapture"
MouseMove="MyIP_MouseMove"
Background="White" Canvas.Top="53" Canvas.Left="3" Opacity="1" >
</InkPresenter>
单击Proceedbutton..我需要捕获Inkpresenter内容并保存到base64字符串
我实现的逻辑
private void ProceedClick(object sender, RoutedEventArgs e)
{
WriteableBitmap wbBitmap = new WriteableBitmap(inkSignature, new TranslateTransform());
EditableImage eiImage = new EditableImage(wbBitmap.PixelWidth, wbBitmap.PixelHeight);
try
{
for (int y = 0; y < wbBitmap.PixelHeight; ++y)
{
for (int x = 0; x < wbBitmap.PixelWidth; ++x)
{
int pixel = wbBitmap.Pixels[wbBitmap.PixelWidth * y + x];
eiImage.SetPixel(x, y,
(byte)((pixel >> 16) & 0xFF),
(byte)((pixel >> 8) & 0xFF),
(byte)(pixel & 0xFF), (byte)((pixel >> 24) & 0xFF)
);
}
}
}
catch (System.Security.SecurityException)
{
throw new Exception("Cannot print images from other domains");
}
// Save it isolated storage
Stream streamPNG = eiImage.GetStream();
StreamReader srPNG = new StreamReader(streamPNG);
byte[] baBinaryData = new Byte[streamPNG.Length];
long bytesRead = streamPNG.Read(baBinaryData, 0, (int)streamPNG.Length);
//Obtain a virtual store for application
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream isfStream = new IsolatedStorageFileStream(ClsCommon.signImage, FileMode.Create, fileStorage);
isfStream.Write(baBinaryData, 0, baBinaryData.Length);
isfStream.Close();
MessageBox.Show("Your signature has been saved. Thank you !", "mPOS", MessageBoxButton.OK);
ConvImagetoBase64();
// this.NavigationService.GoBack();
}
public void ConvImagetoBase64()
{
byte[] data;
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.FileExists(ClsCommon.signImage))
{
using (IsolatedStorageFileStream isfs = store.OpenFile(ClsCommon.signImage, FileMode.Open, FileAccess.Read))
{
// Allocate an array large enough for the entire file
data = new byte[isfs.Length];
// Read the entire file and then close it
isfs.Read(data, 0, data.Length);
isfs.Close();
}
// Create memory stream and bitmap
MemoryStream ms = new MemoryStream(data);
BitmapImage bi = new BitmapImage();
byte[] bytearray = null;
bytearray = ms.ToArray();
string result= Convert.ToBase64String(bytearray);
}
}
}
private void ProceedClick(对象发送方,路由目标)
{
WriteableBitmap wbBitmap=新的WriteableBitmap(inkSignature,new TranslateTransform());
EditableImage eiImage=新的EditableImage(wbBitmap.PixelWidth,wbBitmap.PixelHeight);
尝试
{
对于(int y=0;y>16)和0xFF),
(字节)((像素>>8)和0xFF),
(字节)(像素和0xFF),(字节)(像素>>24)和0xFF)
);
}
}
}
捕获(System.Security.SecurityException)
{
抛出新异常(“无法打印来自其他域的图像”);
}
//将其保存为独立存储
Stream streamPNG=eiImage.GetStream();
StreamReader srPNG=新的StreamReader(streamPNG);
byte[]baBinaryData=新字节[streamPNG.Length];
long bytesRead=streamPNG.Read(baBinaryData,0,(int)streamPNG.Length);
//为应用程序获取虚拟存储
IsolatedStorageFile fileStorage=IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream isfStream=新的IsolatedStorageFileStream(ClsCommon.signImage,FileMode.Create,fileStorage);
写入(baBinaryData,0,baBinaryData.Length);
isfStream.Close();
MessageBox.Show(“您的签名已保存,谢谢!”,“mPOS”,MessageBoxButton.OK);
ConvImagetoBase64();
//this.NavigationService.GoBack();
}
公共无效CONVIGETOBASE64()
{
字节[]数据;
使用(var store=IsolatedStorageFile.GetUserStoreForApplication())
{
if(store.FileExists(ClsCommon.signImage))
{
使用(IsolatedStorageFileStream isfs=store.OpenFile(ClsCommon.signImage,FileMode.Open,FileAccess.Read))
{
//为整个文件分配足够大的数组
数据=新字节[isfs.Length];
//读取整个文件,然后关闭它
读取(数据,0,数据长度);
isfs.Close();
}
//创建内存流和位图
MemoryStream ms=新的MemoryStream(数据);
BitmapImage bi=新的BitmapImage();
字节[]bytearray=null;
bytearray=ToArray女士();
字符串结果=Convert.tobase64字符串(bytearray);
}
}
}
在字符串结果值处调试时=
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////xkZ//8AAP//AAD//t/f/v////////////////19f+/wUF//8AAP//AAD//qOj/v/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////vv7/v8AAP//AAD//wAA//77+/7////////////////0VF//8AAP//AAD//z09/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
这里面有很多。。最后以
/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
我犯了什么错误…请引导我重新评分
如果您有其他简单的方法,请建议……那么,什么方法不起作用呢<代码>/是中的有效字符,因此没有明显的错误。我得到的输出是