问题描述
我有一个基于套接字通信的应用程序,并且服务器向客户端发送请求以获取签名图像.签名在客户端获取,然后发送回服务器.
I have a socket communication based application and the server sends a request to a client for a signature image. The signature is taken at the client and sent back to the server.
为此,我使用了一个图片框,该图片框的数据被转换为字节,然后转换为字符串,以通过套接字连接发送到服务器.一切正常.
To do this I have used a picture box, who's data is converted into bytes and then into a string to send over the socket connection to the server. This all works OK.
我在保存数据库中接收到的数据时遇到问题,因为我需要能够将其保存为图像类型,但是此时数据仍然是字符串,所以出现错误:
I am having problems saving the data received in the database because I need to be able to save it as an image type but at this point the data is still a string so I am getting the error:
操作数类型冲突:nvarchar与图像不兼容"
"Operand type clash: nvarchar is incompatible with image"
我想是因为我试图将字符串发送到sql图像类型吗?
I presume it's because I am trying to send a string to a sql image type?
尝试调用该过程和错误的代码(服务器端接收到的数据作为字符串):
Code which attempts to call the procedure and errors (server end- received data as string):
try
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["GetConnector"].ConnectionString))
{
SqlCommand sqlComm = new SqlCommand("PL_Device_ReadData", conn);
sqlComm.Parameters.AddWithValue("@DeviceTypeID", DeviceID);
sqlComm.Parameters.AddWithValue("@RequestID", RequestID);
if (DeviceID != "5")
{
sqlComm.Parameters.AddWithValue("@Reading", Reading);
}
else if (DeviceID == "5")
{
sqlComm.Parameters.AddWithValue("@ImageReading", Reading);
sqlComm.Parameters.Add("@ImageReading", SqlDbType.Image);
sqlComm.Parameters["@ImageReading"].Value = Reading;
}
sqlComm.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = sqlComm;
da.Fill(ds);
}
}
catch (Exception e)
{
}
存储过程:
ALTER PROCEDURE [dbo].[PL_Device_ReadData]
@DeviceTypeID INT,
@RequestID INT,
@Reading NVARCHAR(100) = NULL,
@ImageReading IMAGE = NULL
AS
BEGIN
--Data
IF @DeviceTypeID = 5
BEGIN
UPDATE dbo.DeviceRequests
SET ImageData = @ImageReading
WHERE id = @RequestID
AND DeviceTypeID = @DeviceTypeID
END
ELSE
BEGIN
UPDATE dbo.DeviceRequests
SET Value = @Reading
WHERE id = @RequestID
AND DeviceTypeID = @DeviceTypeID
END
END
将图像转换为字节:
byte[] imgData = null;
// storage for the img bytes
imgData = ImgToByteArray(picSignature.InitialImage, ImageFormat.Jpeg);
public static byte[] ImgToByteArray(Image img, ImageFormat imgFormat)
{
byte[] tmpData = null;
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, imgFormat);
tmpData = ms.ToArray();
}
// dispose of memstream
return tmpData;
}
将字节转换为字符串(通过套接字发送):
Converting the bytes to string (to send over socket):
_Reading = System.Text.Encoding.UTF8.GetString(_imagevalues);
推荐答案
此:
_Reading = System.Text.Encoding.UTF8.GetString(_imagevalues);
是错误的. _imagevalues
是否保存图像的二进制数据?比起这行代码,您试图将其解释为字符串,这是错误的.您只需要转换每个字节表示其十六进制字符串表示形式的图像.然后通过网络发送整个这样的字符串.例如,请参见此处如何转换字节数组转换为十六进制字符串,反之亦然?(如在此转换的答案之一中所述)并不是必须的,您也可以发送纯字节).
is wrong. Is _imagevalues
holding binary data of the image?Than in that line of code you are trying to interpret this as string,which is wrong. You have to just convert each individual byteof image to say its hexadecimal string representation. Then send the whole such string over network.e.g., see here How do you convert Byte Array to Hexadecimal String, and vice versa? (As noted in one of the answers this conversionsis not really necessary and you could as well send plain bytes).
然后在服务器端,您必须解析此十六进制字符串并将其转换为字节数组,并可能保存在数据库中.
Then on the server side, you have to parse this hexadecimalstring and convert it to byte array and probably save in the database.
这篇关于将图像转换为字节然后转换为字符串时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!