include image bytes into packet c#

01/13/2015 16:48 abdeen#1
Hello everyone. ..

I am working on a client/server application with packets ... that's have an id and packet length so I can receive and understand incoming or outing packets easy ... everything works as well.

But now I'm just trying to send an image from client to server ... I need to make it in packet as I do with others ... how can I do that ?
01/13/2015 17:17 pro4never#2
You need to serialize and deserialize data same as any information you send.


You need to break whatever you're sending down into its base data, send it to the client/server which then needs to process that data back into whatever type of data it needs to be. It's no different than structuring a packet really.
01/13/2015 21:05 kakamankoko#3
convert image to byte array :
Code:
public static byte[] ImageToByte(Image img)
{
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
or use memory stream

Code:
public static byte[] ImageToByte(Image img, ImageFormat format)
    {
        using(MemoryStream ms = new MemoryStream())
        {
            image.Save(ms, format);
            return ms.ToArray();
        }
    }
and then use this code to convert it back to image

Code:
public Image byteArrayToImage(byte[] fileBytes)
{
    using (MemoryStream fileStream = new MemoryStream(fileBytes))
    {
        return Image.FromStream( fileStream );
    }
}
01/13/2015 23:34 abdeen#4
Quote:
Originally Posted by kakamankoko View Post
convert image to byte array :
Code:
public static byte[] ImageToByte(Image img)
{
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
or use memory stream

Code:
public static byte[] ImageToByte(Image img, ImageFormat format)
    {
        using(MemoryStream ms = new MemoryStream())
        {
            image.Save(ms, format);
            return ms.ToArray();
        }
    }
and then use this code to convert it back to image

Code:
public Image byteArrayToImage(byte[] fileBytes)
{
    using (MemoryStream fileStream = new MemoryStream(fileBytes))
    {
        return Image.FromStream( fileStream );
    }
}
Great, thanks

But how to send in a packet after that
01/13/2015 23:45 pro4never#5
Quote:
Originally Posted by abdeen View Post
Great, thanks

But how to send in a packet after that
AHAHAHA

There it is...


He literally told you exact code of how to turn an image into a byte array and then back into an image...


There's no 'converting it into a packet'... a packet IS a group of bits/bytes. You take the data, you send it and you process it. Boom you're done.

Seriously abdeen go fuck yourself and find a different forum already
01/14/2015 02:09 Super Aids#6
An image or any other type of file is just bytes stored on your computer. When you send an image (that is either stored physically as a file or in memory) you take those bytes and send. It's no different than sending any other type of data.

I would suggest using the UDP protocol for the socket that sends the image, if you're going to send multiple images fast over time. If it's once or just not a lot stick with TCP for safety.

Also to answer your last question.
Can't you really figure out how to do this?
Code:
socket.Send(ImageToByte(_image));
Suggestion send the size before. (Use 4 bytes for the size.)
01/14/2015 14:48 abdoumatrix#7
u could convert it to base64 string and send it :D
01/14/2015 15:35 abdeen#8
okay guys i know how to send image and i can use this code

PHP Code:
System.Drawing.Bitmap myimg = new System.Drawing.Bitmap(OriginalImage);
System.Drawing.Image f = (System.Drawing.Image)myimg;
MemoryStream ms = new MemoryStream();
f.save(msSystem.Drawing.Imaging.ImageFormat.jpeg);
client.Send(ms.ToArray()); 
but now i want to make it as a packet class ... can anybody show me a simple image packet class ?
01/14/2015 16:01 turk55#9
Brb, grabbing my spoon.
01/14/2015 17:12 Super Aids#10
Quote:
Originally Posted by abdeen View Post
okay guys i know how to send image and i can use this code

PHP Code:
System.Drawing.Bitmap myimg = new System.Drawing.Bitmap(OriginalImage);
System.Drawing.Image f = (System.Drawing.Image)myimg;
MemoryStream ms = new MemoryStream();
f.save(msSystem.Drawing.Imaging.ImageFormat.jpeg);
client.Send(ms.ToArray()); 
but now i want to make it as a packet class ... can anybody show me a simple image packet class ?
First of all why do you make a bitmap from an image and then converts the bitmap back to an image. Get rid of the bitmap.

Secondly you're not disposing any data. Image data (Bitmaps etc. always has to be disposed after usage, otherwise you'll end up with memory leaks.) and the same goes for streams in this case your memory stream.

Either you call Dispose() or you wrap them inside of a using statement.
Ex.
Code:
using (var ms = new MemoryStream()) {
    // do shit with ms ....
}
01/14/2015 19:18 abdeen#11
Quote:
Originally Posted by Super Aids View Post
First of all why do you make a bitmap from an image and then converts the bitmap back to an image. Get rid of the bitmap.

Secondly you're not disposing any data. Image data (Bitmaps etc. always has to be disposed after usage, otherwise you'll end up with memory leaks.) and the same goes for streams in this case your memory stream.

Either you call Dispose() or you wrap them inside of a using statement.
Ex.
Code:
using (var ms = new MemoryStream()) {
    // do shit with ms ....
}
Thank you for your help.

I wish if you tell me how to send image in a packet class ... just like 1001 Conquer online packet which have packet id and packet length ... so we can control them easily. ..

I can create a packet classes and send them to server or client ... but with string, int, double .... etc ... but how to send image bytes instead of string or uint or anything else ... that's and I know the image size is bigger than maximum bytes of packet length in tcp servers ....
01/14/2015 19:38 Super Aids#12
... :facepalm:
Code:
// Size is 6 + image bytes, because 4 for packet size and 2 for packet type
Packet.WriteUInt32((uint)imageBytes.Length + 6); // use 4 bytes for image size ...
Packet.WriteUInt16(PacketType.Image);
Packet.WriteBytes(imageBytes);
01/14/2015 19:50 Best Coder 2014#13
Just stop yourself, seriously. You obviously don't understand anything about the fundamentals of programming. And people, please, enough with the spoonfeeding.
01/15/2015 11:19 abdeen#14
Quote:
Originally Posted by Super Aids View Post
... :facepalm:
Code:
// Size is 6 + image bytes, because 4 for packet size and 2 for packet type
Packet.WriteUInt32((uint)imageBytes.Length + 6); // use 4 bytes for image size ...
Packet.WriteUInt16(PacketType.Image);
Packet.WriteBytes(imageBytes);
thanks man, with your help now i can make this packet for the client :

PHP Code:
public class Image WriterInterfaces.IPacket
{
public 
byte[] Buffer;
public 
byte[] ImageBytes;

public 
Image()
{
}

public 
void Deserialize(byte[] buffer)
{
Buffer buffer;
}

public 
byte[] ToArray()
{
Buffer = new byte[(ImageBytes) + 6];
Writer.WriteUInt16(10030 Buffer);
Writer.WriteUint16((ushort)(Buffer.Length), 2Buffer);
Writer.WriteBytes(ImageBytes4Buffer);
return 
Buffer;
}

and i receive the packet 1003 in the server but with different lengths , so i don't switch lengths ... so how the image packet class will be in the server to get the only image bytes from the whole packet to reconvert the bytes to image again ?

is there a way or i have to trim first 6 bytes from the packet [ Header ] or what ?
01/15/2015 16:25 Super Aids#15
Well a 16 bit int length will only work with relative small images. If you want to use regular sized / a little big images you have to use a 32 bit int for the length of it.

Which means an uint for the length.

But I give up....