图片转换为字节流,字节流转换为图片

图片-》文件流-》字节流

//比如得到图片全路径名称 fileName

FileStream fs=new FileStream(fileName,FileMode.Open,FileAccess.Read);

fs.read(字节数组,位置,长度);

图片-》内存流-》字节流

Image ima=Image.FromFile(图片全路径名称);

ima.save(内存流ms,格式);

ms.toArray()


一,

OpenFileDialog ofd = new OpenFileDialog();
        private void btnUpload_Click(object sender, System.EventArgs e)
        {

            ofd.Filter = "JPEG|*.jpeg;*.jpg|Bitmap|*.bmp;*.dib|GIF|*.gif;*.gfa|Portable Network Graphics|*.png|Windows Metafile|*.wmf|Windows Enhanced Metafile|*.emf";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                if (ofd.FileName != null)
                {
                    this.imgPicture.Image = Image.FromFile(ofd.FileName);


                    //转换为byte类型,保存

                    MemoryStream ms = new MemoryStream();
                    this.imgPicture.Image.Save(ms, imgPicture.Image.RawFormat);
                    image = ms.ToArray(); //赋值byte                   

二,

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Multiselect=true;
            if(openFileDialog.ShowDialog()==DialogResult.OK)
            {
                fileName= openFileDialog.FileNames;
            }

    FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            byte[] bytes = new byte[fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            return bytes;

三,

//把byte[] 字节数组转换为image

private Image parseBytesToImage(byte[] bytesArg) 
        {
            MemoryStream ms = new MemoryStream(bytesArg);
            Image ima = Image.FromStream(ms);

    ima.Save(@"C:\Users\Administrator\Desktop\work\pic\111\"+imageFile);
            return ima;
        }


你可能感兴趣的:(c#)