GDI+;中发生一般错误;使用带有内存流的c#在控制台应用程序中保存图像时
当我试图在c#中使用memorystream保存图像时,出现了下面提到的错误。只有当字节数组byteImageData长度等于24000时才会引发错误,否则代码工作正常。下面是代码片段:GDI+;中发生一般错误;使用带有内存流的c#在控制台应用程序中保存图像时,c#,image,save,C#,Image,Save,当我试图在c#中使用memorystream保存图像时,出现了下面提到的错误。只有当字节数组byteImageData长度等于24000时才会引发错误,否则代码工作正常。下面是代码片段: string strDbConn = string.Empty; string strImageFileName = string.Empty; string strImageData = string.Empty; DataSet imageDS = new DataSet(); Byte[] byteIma
string strDbConn = string.Empty;
string strImageFileName = string.Empty;
string strImageData = string.Empty;
DataSet imageDS = new DataSet();
Byte[] byteImageData = new Byte[0];
Image saveImage;
string strImgSavePath = string.Empty;
try
{
//---open the database connection
strDbConn = ConfigurationSettings.AppSettings["DBConnection"].ToString().Trim();
SqlConnection dbcon = new SqlConnection(strDbConn);
dbcon.Open();
SqlDataAdapter imageSqlDataAdapter = new SqlDataAdapter("select * from image_data", dbcon);
imageSqlDataAdapter.Fill(imageDS);
dbcon.Close();
for (int i = 0; i < imageDS.Tables[0].Rows.Count; i++)
{
strImageFileName = imageDS.Tables[0].Rows[i]["name"].ToString().Trim();
strImageData = imageDS.Tables[0].Rows[i]["signature_vod__c"].ToString().Trim();
//converting string to byte array
byteImageData = Convert.FromBase64String(strImageData);
//---create Memory stremm from the Image Byte data
MemoryStream stmImageData = new MemoryStream(byteImageData);
//--saving the image
saveImage = Image.FromStream(stmImageData);
strImgSavePath = ConfigurationSettings.AppSettings["ImageSavePath"].ToString().Trim();
saveImage.Save(strImgSavePath + strImageFileName + ".jpeg"); // error comes here
//stmImageData.Close();
}
}
catch (Exception ex)
{
}
堆栈跟踪:
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at System.Drawing.Image.Save(String filename)
at VeevaImageApp.Program.CreateAndSaveImageusingDataSet2() in Program.cs:line 141
只有当Byte array byteImageData长度为24000时,才会出现错误,否则代码将在文件中工作-可能存在什么问题,我如何解决它?图像数据的格式是什么?如果它已经是JPG,您应该只使用
File.writealBytes(path,byteImageData)
。如果您确实需要转换,您应该使用更新的图像编码器/解码器类:在web上搜索有关JpegBitmapEncoder或PngBitmapEncoder的信息。(如果这些是照片或扫描,请使用JPEG,否则请使用PNG。)您确定图像数据正确吗?如果将二进制数据保存到一个名为test.jpeg的文件中,它的渲染是否正常?您是否有权限写入要保存到的位置?在DataSet中,我有5000个图像,它在509个图像之前一直工作,在这之后,当我得到第510个图像字节数组长度达到24000时,它给了我错误,时间是的,我有权写入位置,代码能够写入509个图像,但当byteImageData长度为24000时会出现错误。在循环之前,您是否尝试过在转换strImageData
时使用隐式声明,例如var byteImageData=convert.FromBase64String(strImageData)代码>?
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at System.Drawing.Image.Save(String filename)
at VeevaImageApp.Program.CreateAndSaveImageusingDataSet2() in Program.cs:line 141