MemoryStream
How do I use Memory Stream
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: CustomTypes.cs
Copy
45 private static short SerializeVector3(MemoryStream outStream, object customobject)
46 {
47 Vector3 vo = (Vector3)customobject;
48
49 int index = 0;
50 lock (memVector3)
51 {
52 byte[] bytes = memVector3;
53 Protocol.Serialize(vo.x, bytes, ref index);
54 Protocol.Serialize(vo.y, bytes, ref index);
55 Protocol.Serialize(vo.z, bytes, ref index);
56 outStream.Write(bytes, 0, 3 * 4);
57 }
58
59 return 3 * 4;
60 }
File name: CustomTypes.cs
Copy
62 private static object DeserializeVector3(MemoryStream inStream, short length)
63 {
64 Vector3 vo = new Vector3();
65 lock (memVector3)
66 {
67 inStream.Read(memVector3, 0, 3 * 4);
68 int index = 0;
69 Protocol.Deserialize(out vo.x, memVector3, ref index);
70 Protocol.Deserialize(out vo.y, memVector3, ref index);
71 Protocol.Deserialize(out vo.z, memVector3, ref index);
72 }
73
74 return vo;
75 }
File name: CustomTypes.cs
Copy
79 private static short SerializeVector2(MemoryStream outStream, object customobject)
80 {
81 Vector2 vo = (Vector2)customobject;
82 lock (memVector2)
83 {
84 byte[] bytes = memVector2;
85 int index = 0;
86 Protocol.Serialize(vo.x, bytes, ref index);
87 Protocol.Serialize(vo.y, bytes, ref index);
88 outStream.Write(bytes, 0, 2 * 4);
89 }
90
91 return 2 * 4;
92 }
File name: CustomTypes.cs
Copy
94 private static object DeserializeVector2(MemoryStream inStream, short length)
95 {
96 Vector2 vo = new Vector2();
97 lock (memVector2)
98 {
99 inStream.Read(memVector2, 0, 2 * 4);
100 int index = 0;
101 Protocol.Deserialize(out vo.x, memVector2, ref index);
102 Protocol.Deserialize(out vo.y, memVector2, ref index);
103 }
104
105 return vo;
106 }
File name: CustomTypes.cs
Copy
110 private static short SerializeQuaternion(MemoryStream outStream, object customobject)
111 {
112 Quaternion o = (Quaternion)customobject;
113
114 lock (memQuarternion)
115 {
116 byte[] bytes = memQuarternion;
117 int index = 0;
118 Protocol.Serialize(o.w, bytes, ref index);
119 Protocol.Serialize(o.x, bytes, ref index);
120 Protocol.Serialize(o.y, bytes, ref index);
121 Protocol.Serialize(o.z, bytes, ref index);
122 outStream.Write(bytes, 0, 4 * 4);
123 }
124
125 return 4 * 4;
126 }
File name: CustomTypes.cs
Copy
128 private static object DeserializeQuaternion(MemoryStream inStream, short length)
129 {
130 Quaternion o = new Quaternion();
131
132 lock (memQuarternion)
133 {
134 inStream.Read(memQuarternion, 0, 4 * 4);
135 int index = 0;
136 Protocol.Deserialize(out o.w, memQuarternion, ref index);
137 Protocol.Deserialize(out o.x, memQuarternion, ref index);
138 Protocol.Deserialize(out o.y, memQuarternion, ref index);
139 Protocol.Deserialize(out o.z, memQuarternion, ref index);
140 }
141
142 return o;
143 }
File name: CustomTypes.cs
Copy
146 private static short SerializePhotonPlayer(MemoryStream outStream, object customobject)
147 {
148 int ID = ((PhotonPlayer)customobject).ID;
149
150 lock (memPlayer)
151 {
152 byte[] bytes = memPlayer;
153 int off = 0;
154 Protocol.Serialize(ID, bytes, ref off);
155 outStream.Write(bytes, 0, 4);
156 return 4;
157 }
158 }
File name: CustomTypes.cs
Copy
160 private static object DeserializePhotonPlayer(MemoryStream inStream, short length)
161 {
162 int ID;
163 lock (memPlayer)
164 {
165 inStream.Read(memPlayer, 0, length);
166 int off = 0;
167 Protocol.Deserialize(out ID, memPlayer, ref off);
168 }
169
170 if (PhotonNetwork.networkingPeer.mActors.ContainsKey(ID))
171 {
172 return PhotonNetwork.networkingPeer.mActors[ID];
173 }
174 else
175 {
176 return null;
177 }
178 }
File name: frmProduct.cs
Copy
96 private void btnSave_Click(object sender, EventArgs e)
97 {
98 if (txtProductName.Text == "")
99 {
100 MessageBox.Show("Please enter product name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
101 txtProductName.Focus();
102 return;
103 }
104 if (cmbCategory.Text == "")
105 {
106 MessageBox.Show("Please select category", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
107 cmbCategory.Focus();
108 return;
109 }
110 if (cmbSubCategory.Text == "")
111 {
112 MessageBox.Show("Please select sub category", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
113 cmbSubCategory.Focus();
114 return;
115 }
116 if (txtPrice.Text == "")
117 {
118 MessageBox.Show("Please enter price", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
119 txtPrice.Focus();
120 return;
121 }
122 try
123 {
124 con = new SqlConnection(cs.DBConn);
125 con.Open();
126 string ct = "select ProductName from Product where ProductName='" + txtProductName.Text + "'";
127
128 cmd = new SqlCommand(ct);
129 cmd.Connection = con;
130 rdr = cmd.ExecuteReader();
131
132 if (rdr.Read())
133 {
134 MessageBox.Show("Product Name Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
135 txtProductName.Text = "";
136 txtProductName.Focus();
137
138
139 if ((rdr != null))
140 {
141 rdr.Close();
142 }
143 return;
144 }
145 auto();
146 con = new SqlConnection(cs.DBConn);
147 con.Open();
148 string cb = "insert into Product(ProductID,ProductName,CategoryID,SubCategoryID,Features,Price,Image) VALUES ('" + txtProductID.Text + "','" + txtProductName.Text + "'," + txtCategoryID.Text+ "," + txtSubCategoryID.Text+ ",@d1,"+ txtPrice.Text +",@d2)";
149 cmd = new SqlCommand(cb);
150 cmd.Connection = con;
151 cmd.Parameters.AddWithValue("@d1", txtFeatures.Text);
152 MemoryStream ms = new MemoryStream();
153 Bitmap bmpImage = new Bitmap(pictureBox1.Image);
154 bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
155 byte[] data = ms.GetBuffer();
156 SqlParameter p = new SqlParameter("@d2", SqlDbType.Image);
157 p.Value = data;
158 cmd.Parameters.Add(p);
159 cmd.ExecuteReader();
160 con.Close();
161 MessageBox.Show("Successfully saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
162 Autocomplete();
163 btnSave.Enabled = false;
164 }
165 catch (Exception ex)
166 {
167 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
168 }
169 }
File name: frmProduct.cs
Copy
244 private void btnUpdate_Click(object sender, EventArgs e)
245 {
246 if (txtProductName.Text == "")
247 {
248 MessageBox.Show("Please enter product name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
249 txtProductName.Focus();
250 return;
251 }
252 if (cmbCategory.Text == "")
253 {
254 MessageBox.Show("Please select category", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
255 cmbCategory.Focus();
256 return;
257 }
258 if (cmbSubCategory.Text == "")
259 {
260 MessageBox.Show("Please select sub category", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
261 cmbSubCategory.Focus();
262 return;
263 }
264 if (txtPrice.Text == "")
265 {
266 MessageBox.Show("Please enter price", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
267 txtPrice.Focus();
268 return;
269 }
270 try
271 {
272
273 con = new SqlConnection(cs.DBConn);
274 con.Open();
275 string cb = "Update product set ProductName='" + txtProductName.Text + "',CategoryID=" + txtCategoryID.Text + ",SubCategoryID=" + txtSubCategoryID.Text + ",Features=@d1,price=" + txtPrice.Text + ",Image=@d2 Where ProductID='" + txtProductID.Text + "'";
276 cmd = new SqlCommand(cb);
277 cmd.Connection = con;
278 cmd.Parameters.AddWithValue("@d1", txtFeatures.Text);
279 MemoryStream ms = new MemoryStream();
280 Bitmap bmpImage = new Bitmap(pictureBox1.Image);
281 bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
282 byte[] data = ms.GetBuffer();
283 SqlParameter p = new SqlParameter("@d2", SqlDbType.Image);
284 p.Value = data;
285 cmd.Parameters.Add(p);
286 cmd.ExecuteReader();
287 con.Close();
288 MessageBox.Show("Successfully updated", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
289 Autocomplete();
290 btnUpdate.Enabled = false;
291 }
292 catch (Exception ex)
293 {
294 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
295 }
296 }
MemoryStream 223 lượt xem
Gõ tìm kiếm nhanh...