如何将组成文件的原始位显示为位图图像? [关闭]

问题描述 投票:-2回答:1

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

c# file bmp
1个回答
1
投票

人们可以做到以下几点:

        public void Convert()
        {
            string filePath = "C:\\Users\\User\\Desktop\\sample.mid";
            byte[] bytes = File.ReadAllBytes(filePath);
            int pixelCount = (int)Math.Ceiling(bytes.Count() / 4.0);
            double width = Math.Ceiling(Math.Sqrt(pixelCount));
            double height = Math.Ceiling(pixelCount / width);
            string outputPath = "C:\\Users\\User\\Desktop\\sample.bmp";
            SaveAsBmp((int)width, (int)height, bytes, outputPath);
        }

        public void SaveAsBmp(int width, int height, byte[] argbData, string path)
        {
            using (Bitmap img = new Bitmap(width, height, PixelFormat.Format32bppPArgb))
            {
                BitmapData data = img.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, img.PixelFormat);
                for (int y = 0; y < height; y++)
                {
                    Marshal.Copy(argbData, width * y, data.Scan0 + data.Stride * y, width * 4);
                }
                img.UnlockBits(data);
                img.Save(path, ImageFormat.Bmp);
            }
        }

以下是从这里提供的样本MIDI获得的:https://en.wikipedia.org/wiki/File:MIDI_sample.mid?qsrc=3044

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.