|
You last visited: Today at 15:44
Advertisement
include image bytes into packet c#
Discussion on include image bytes into packet c# within the CO2 Private Server forum part of the Conquer Online 2 category.
01/13/2015, 16:48
|
#1
|
elite*gold: 0
Join Date: Mar 2010
Posts: 475
Received Thanks: 15
|
include image bytes into packet c#
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
|
#2
|
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,380
|
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
|
#3
|
elite*gold: 0
Join Date: Apr 2011
Posts: 93
Received Thanks: 20
|
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
|
#4
|
elite*gold: 0
Join Date: Mar 2010
Posts: 475
Received Thanks: 15
|
Quote:
Originally Posted by kakamankoko
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
|
#5
|
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,380
|
Quote:
Originally Posted by abdeen
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 **** yourself and find a different forum already
|
|
|
01/14/2015, 02:09
|
#6
|
elite*gold: 0
Join Date: Dec 2012
Posts: 1,761
Received Thanks: 950
|
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
|
#7
|
elite*gold: 0
Join Date: Jul 2008
Posts: 874
Received Thanks: 238
|
u could convert it to base64 string and send it
|
|
|
01/14/2015, 15:35
|
#8
|
elite*gold: 0
Join Date: Mar 2010
Posts: 475
Received Thanks: 15
|
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(ms, System.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
|
#9
|
elite*gold: 130
Join Date: Oct 2007
Posts: 1,655
Received Thanks: 705
|
Brb, grabbing my spoon.
|
|
|
01/14/2015, 17:12
|
#10
|
elite*gold: 0
Join Date: Dec 2012
Posts: 1,761
Received Thanks: 950
|
Quote:
Originally Posted by abdeen
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(ms, System.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
|
#11
|
elite*gold: 0
Join Date: Mar 2010
Posts: 475
Received Thanks: 15
|
Quote:
Originally Posted by Super Aids
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 **** 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
|
#12
|
elite*gold: 0
Join Date: Dec 2012
Posts: 1,761
Received Thanks: 950
|
...
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
|
#13
|
elite*gold: 0
Join Date: Jul 2014
Posts: 402
Received Thanks: 540
|
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
|
#14
|
elite*gold: 0
Join Date: Mar 2010
Posts: 475
Received Thanks: 15
|
Quote:
Originally Posted by Super Aids
...
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 : Writer, Interfaces.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(1003, 0 Buffer);
Writer.WriteUint16((ushort)(Buffer.Length), 2, Buffer);
Writer.WriteBytes(ImageBytes, 4, Buffer);
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
|
#15
|
elite*gold: 0
Join Date: Dec 2012
Posts: 1,761
Received Thanks: 950
|
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....
|
|
|
 |
|
Similar Threads
|
[VB] TCP Packet als Bytes senden
04/25/2014 - .NET Languages - 3 Replies
Guten Abend,
ich versuche einem Server per TCP ein Packet für einen Request von meinen Accountdaten zu schicken, die benötigten Packets habe ich bereits gefiltert, analysiert und nachgebaut, einziges Problem ist, der Header des Packets, welches ich an den Server schicke ist ein Kauderwelsch wenn ich ihn als Ascii oder Dezimalzahl darstelle, das heißt er muss als ein Bytearray verschickt werden.
Mein Problem ist jetzt, dass die anderen Daten als String gespeichert werden und ich zwar die...
|
How to parse Image Code Packet?
03/17/2012 - SRO Coding Corner - 2 Replies
My clientless get (opcode=2322) image code packet after login~
Does anyone know how to parse it and make it show on screen?
Searched through the forum but lacking of such information~
|
(Winsock hook) replace/filter packet bytes
11/18/2010 - General Coding - 3 Replies
Hi guys
Please, can someone explain me or help me about my winsock hook?!
I need to change the first and second bytes of the packet I've received...
example:
I'm receiving the packet:
|
2 Bytes oder 4 Bytes ?
02/13/2010 - Kal Online - 3 Replies
Erm wenn ich nach cooldowns schaue für Mockery mit der UCE
such ich dann mit 2 Bytes oder 4 Bytes ??
|
Interlude 11 bytes init packet wtf??
02/07/2008 - Lineage 2 - 0 Replies
Hello!
in some interlude servers, first packet(init) length is about 11 bytes, when im expecting a more huge packet(about 186 bytes) containing:
protocolVersion (Integer)
sessionId(Integer)
rsaPublicKey (byte)
blowfishKey (byte)
gg1 (byte)
gg2 (byte)
gg3 (byte)
|
All times are GMT +1. The time now is 15:45.
|
|