如何将新的字节数组转换为图像

编程入门 行业动态 更新时间:2024-10-28 18:24:07
本文介绍了如何将新的字节数组转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已编写此代码用于提取图像的bitplane1。但我有例外

i have write this code for extracting bitplane1 of my image . but i have exception

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Collections; namespace bitplane { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // Image grayImage; OpenFileDialog o = new OpenFileDialog(); o.ShowDialog(); byte[] x = File.ReadAllBytes(o.FileName); byte maskbyte1 = 2; int [] newpix= new int [x.Length]; for (int i = 0; i < x.Length; i++) { newpix[i] = x[i] & maskbyte1; string px=newpix[i].ToString(); x[i] = Convert.ToByte(px); } MemoryStream ms = new MemoryStream(x); Image myImage = Image.FromStream(ms); myImage.Save(@"C:\Users\Public\Pictures\Sample Pictures\New folder\fgh.jpg"); } } }

推荐答案

使用httphandler中的以下代码将字节数组转换为图片 use the below code in httphandler to convert byte array to image context.Response.ContentType = "image/jpeg";//get image content type of selected file Stream strm = new MemoryStream(x); byte[] buffer = new byte[4096]; int byteSeq = strm.Read(buffer, 0, 4096); while (byteSeq > 0) { context.Response.OutputStream.Write(buffer, 0, byteSeq); byteSeq = strm.Read(buffer, 0, 4096); }

您使用的方法正确,但只有两行与 MemoryStream相关以及从文件中读取字节的片段。其他一切都是我无法理解的一些胡言乱语。扔掉它,你将有一个有效的解决方案。如果您遇到问题,请使用调试器。
-SA
You are using correct approach, but only in two lines related to MemoryStream and the fragment where you read bytes from a file. Everything else is some gibberish I cannot understand. Throw it out and you will have a working solution. If you have some problems, use the debugger.
—SA

更多推荐

如何将新的字节数组转换为图像

本文发布于:2023-11-24 19:30:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626520.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   转换为   字节   如何将   图像

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!