如何将构成文件的原始位显示为位图图像?

我想通过将文件转换为字节数组并从字节构建位图来将文件(例如 MIDI 文件)显示为位图。



临摹微笑
浏览 106回答 1
1回答

拉风的咖菲猫

可以执行以下操作:&nbsp; &nbsp; &nbsp; &nbsp; public void Convert()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string filePath = "C:\\Users\\User\\Desktop\\sample.mid";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] bytes = File.ReadAllBytes(filePath);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int pixelCount = (int)Math.Ceiling(bytes.Count() / 4.0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double width = Math.Ceiling(Math.Sqrt(pixelCount));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double height = Math.Ceiling(pixelCount / width);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string outputPath = "C:\\Users\\User\\Desktop\\sample.bmp";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SaveAsBmp((int)width, (int)height, bytes, outputPath);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void SaveAsBmp(int width, int height, byte[] argbData, string path)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (Bitmap img = new Bitmap(width, height, PixelFormat.Format32bppPArgb))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BitmapData data = img.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, img.PixelFormat);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int y = 0; y < height; y++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Marshal.Copy(argbData, width * y, data.Scan0 + data.Stride * y, width * 4);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; img.UnlockBits(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; img.Save(path, ImageFormat.Bmp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }以下是从此处提供的示例 MIDI 中获得的:https ://en.wikipedia.org/wiki/File:MIDI_sample.mid?qsrc=3044
打开App,查看更多内容
随时随地看视频慕课网APP