以下代码退出后如何关闭文件

时间:2019-02-11 00:43:14

标签: c#

以下代码使我的输入文件保持锁定状态。我需要弄清楚在绘制图像后如何释放文件。我称其为绘制波形,它为正弦波创建了一个bmp,但它锁定了文件,从而导致文件使用错误。完成绘制后如何关闭文件的任何想法。为简单起见,我添加了一个按钮,以准确显示我在做什么。我仍然收到文件使用错误。抱歉,我已经将float数组代码添加到开头,这也是使用vs2017的一种胜利形式,float数组与LoadwaveForm代码在同一代码中

    public float[] FloatArrayFromStream(System.IO.MemoryStream stream)
    {
        return FloatArrayFromByteArray(stream.GetBuffer());
    }

    public float[] FloatArrayFromByteArray(byte[] input)
    {
        float[] output = new float[input.Length / 4];
        for (int i = 0; i < output.Length; i++)
        {
            output[i] = BitConverter.ToSingle(input, i * 4);
        }
        return output;
    }
  private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog open = new OpenFileDialog();
        if (open.ShowDialog() != DialogResult.OK) return;
        if (!open.FileName.EndsWith(".mp3")) { return; }
        String FilePath = open.FileName;
        LoadWaveForm(FilePath);
        File.Delete(FilePath);
    }

 private void LoadWaveForm(String MP3FilePath)
    {
        string strPath = MP3FilePath;
        string SongID = "2";
        byte[] bytes = File.ReadAllBytes(strPath);
        WriteToFile(SongID, strPath, bytes);
    }

    private void WriteToFile(string SongID, string strPath, byte[] Buffer)
    {
        try
        {
            int samplesPerPixel = 128;
            long startPosition = 0;
            float[] data = FloatArrayFromByteArray(Buffer);

            Bitmap bmp = new Bitmap(594, 92);

            int BORDER_WIDTH = 5;
            int width = bmp.Width - (2 * BORDER_WIDTH);
            int height = bmp.Height - (2 * BORDER_WIDTH);

            NAudio.Wave.Mp3FileReader reader = new NAudio.Wave.Mp3FileReader(strPath, wf => new NAudio.FileFormats.Mp3.DmoMp3FrameDecompressor(wf));
            NAudio.Wave.WaveChannel32 channelStream = new NAudio.Wave.WaveChannel32(reader);

            int bytesPerSample = (reader.WaveFormat.BitsPerSample / 8) * channelStream.WaveFormat.Channels;

            using (Graphics g = Graphics.FromImage(bmp))
            {

                g.Clear(Color.White);
                Pen pen1 = new Pen(Color.Gray);
                int size = data.Length;

                string hexValue1 = "#009adf";
                Color colour1 = System.Drawing.ColorTranslator.FromHtml(hexValue1);
                pen1.Color = colour1;

                Stream wavestream = new NAudio.Wave.Mp3FileReader(strPath, wf => new NAudio.FileFormats.Mp3.DmoMp3FrameDecompressor(wf));

                wavestream.Position = 0;
                int bytesRead1;
                byte[] waveData1 = new byte[samplesPerPixel * bytesPerSample];
                wavestream.Position = startPosition + (width * bytesPerSample * samplesPerPixel);

                for (float x = 0; x < width; x++)
                {
                    short low = 0;
                    short high = 0;
                    bytesRead1 = wavestream.Read(waveData1, 0, samplesPerPixel * bytesPerSample);
                    if (bytesRead1 == 0)
                        break;
                    for (int n = 0; n < bytesRead1; n += 2)
                    {
                        short sample = BitConverter.ToInt16(waveData1, n);
                        if (sample < low) low = sample;
                        if (sample > high) high = sample;
                    }
                    float lowPercent = ((((float)low) - short.MinValue) / ushort.MaxValue);
                    float highPercent = ((((float)high) - short.MinValue) / ushort.MaxValue);
                    float lowValue = height * lowPercent;
                    float highValue = height * highPercent;
                    g.DrawLine(pen1, x, lowValue, x, highValue);
                }

            }

            pictureBox1.Image = bmp;
        }
        catch
        {

        }
    }

0 个答案:

没有答案