简体   繁体   English

将位图图像转换为字符串格式以通过网络(LAN)发送,反之亦然

[英]Convert Bitmap image to String format to send over network(LAN) and vice-versa

I am basically developing a software in Visual Studio 2010 .NET 4.0 where in I capture the screenshot from a pc and send it over a network to another. 我基本上是在Visual Studio 2010 .NET 4.0中开发一个软件,在该软件中,我从一台PC捕获屏幕截图并将其通过网络发送给另一个。 Since I cannot directly send a Bitmap, I have to convert it to String. 由于我无法直接发送位图,因此必须将其转换为字符串。 I did a lot of internet search but cant find any solution. 我做了很多互联网搜索,但是找不到任何解决方案。 :( :(

I found this code on stackoverflow itself. 我在stackoverflow本身上找到了此代码。 But it it doesnt work. 但是它不起作用。 I tried to print the string (converted from image) but the program behaves like that line doesnt exist. 我试图打印字符串(从图像转换),但是程序的行为就像该行不存在。 I used a MessageBox.Show(String); 我用了一个MessageBox.Show(String); But not even a msg box pops up! 但是,甚至没有一个味精盒弹出! Can anyone please help? 谁能帮忙吗? I'm stuck! 我被卡住了! Thankx in advance :) (Y) 预先感谢:)(是)

bitmapString = null;       // Conversion from image to string
MemoryStream memoryStream = new MemoryStream();
bmpScreenshot.Save(memoryStream, ImageFormat.Png);
byte[] bitmapBytes = memoryStream.GetBuffer();
bitmapString = Convert.ToBase64String(bitmapBytes,Base64FormattingOptions.InsertLineBreaks); // Conversion from image to string end

Image img = null;                           //Conversion from string to image
byte[] bitmapBytes = Convert.FromBase64String(rob);
MemoryStream memoryStream = new MemoryStream(bitmapBytes);
img = Image.FromStream(memoryStream);   //Conversion from string to image end

Try to convert it to a byte array: 尝试将其转换为字节数组:

public static byte[] ImageToByteArray(Image img)
{
    byte[] byteArray = new byte[0];
    using (MemoryStream stream = new MemoryStream())
    {
        img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        stream.Close();

        byteArray = stream.ToArray();
    }
    return byteArray;
}

I believe you can simply cast a Bitmap object to an Image object. 我相信您可以将Bitmap对象简单地转换为Image对象。 So Image img = (Image)myBitmap; 所以Image img = (Image)myBitmap; - then pass that into the method above. -然后将其传递给上面的方法。

Why does it need to be a string? 为什么它需要是一个字符串? What method are you using to send it over the network? 您使用什么方法通过网络发送它? A webservice? 网络服务? Direct sockets? 直接插座?

Regardless of how you're sending it though, the best way would be to convert it to a byte array and then pass that array over the network 不管您如何发送,最好的方法是将其转换为字节数组,然后通过网络传递该数组

If you need some code of how to do this, check out similar questions on SO, like Sending and receiving an image over sockets with C# 如果您需要一些执行此操作的代码,请查看关于SO的类似问题,例如使用C#通过套接字发送和接收图像

You can directly send the individual bytes, but if you really want a string you can encode it in a format called base64 . 您可以直接发送各个字节,但是如果您确实想要一个字符串,则可以使用一种称为base64的格式对其进行编码。 Here's the msdn documentation for encoding to and decoding from this format. 这是msdn文档,用于对此格式进行编码解码 You can convert the image to a byte array using the code @AdamPlocher posted in his answer (which I +1'ed as he saved me from doing it ;) ) 您可以使用张贴在他的答案中的@AdamPlocher代码将图像转换为字节数组(我对其+1了,因为他免除了我这样做;))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM