如何使用此加密代码在表单中添加图像链接?

时间:2014-09-30 07:28:54

标签: c# image winforms encryption

这段代码怎么能在这里转换图像呢?如何从bin文件夹中的文件夹定义图像" ch / 1.jpg" 任何人都可以帮我解决这个问题吗? 我有20张照片和20张表格......

private static byte[] ConvertImageToByteArray(System.Drawing.Image imageToConvert, 
ImageFormat formatOfImage) 
{ 
byte[] Ret; 

try 
{ 

using (MemoryStream ms = new MemoryStream()) 
{ 
imageToConvert.Save(ms,formatOfImage); 
Ret = ms.ToArray(); 
} 
} 
catch (Exception) { throw;} 

return Ret; 
} 


//When you are ready to convert the byte array back 
//to an image, you can include the following code 
//in your method. 

System.Drawing.Image newImage; 


using (MemoryStream ms = new MemoryStream(myByteArray,0,myByteArray.Length)) 
{ 

ms.Write(myByteArray,0,myByteArray.Length); 

newImage = Image.FromStream(ms,true); 

// work with image here. 

// You'll need to keep the MemoryStream open for 
// as long as you want to work with your new image. 

} 

1 个答案:

答案 0 :(得分:0)

如果要从文件加载图像,只需使用:

Image img = Image.FromFile("C:\mypath\myimage.png");  //load the file as image
pictureBox1.Image = img;  //use the image how you like

您不需要完成将图像信息转换为Byte[]的过程,除非您将其存储在某个位置,例如在数据库中。