简体   繁体   English

将图像读取到字节数组并进行更改

[英]Reading image to byte array and changing it

I am trying to work with MemoryStream in C#. 我正在尝试在C#中使用MemoryStream。 So, I do converting Image to byte array and byte array to Image. 因此,我确实将Image转换为字节数组,并将字节数组转换为Image。 Like this 像这样

var bytes = File.ReadAllBytes("1.jpg");
var ms = new MemoryStream(bytes, true);
var image = Image.FromStream(ms);
pictureBox1.Image = image;

It works fine (displaying image in picture box). 它工作正常(在图片框中显示图像)。 But when i try to change byte array 但是当我尝试更改字节数组时

bytes[0] = 254

It throws exception invalid parameter. 抛出异常无效参数。 So, how I can change byte array or write bytes to stream? 那么,如何更改字节数组或将字节写入流? ms.WriteByte not works. ms.WriteByte不起作用。 Thanks 谢谢

JPEG (JPG) file is compressed representation of an image. JPEG(JPG)文件是图像的压缩表示形式。 It has complex structure, it is not byte matrix. 它具有复杂的结构,它不是字节矩阵。 Modification of first byte in it will most likely render it invalid. 修改其中的第一个字节很可能使其无效。 You better apply your modifications to image . 您最好将修改应用于image And then save image as JPEG. 然后将图像另存为JPEG。

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

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