these 2 codes are smiliar but the results are different.

lovejoy226

New member
Joined
Mar 31, 2016
Messages
4
Programming Experience
10+
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Pen pen = new Pen(ForeColor);
            if (m_DrawWave)
            {
                sbpMainPanel.Text = "Drawing .WAV file...";

                wave.Draw(e, pen);

                sbpMainPanel.Text = "Finished drawing .WAV file...";
            }
        }
 public void Draw( PaintEventArgs pea, Pen pen )
        {
            Graphics grfx = pea.Graphics;

            if ( m_PageScale == 0.0f )
                m_PageScale = grfx.VisibleClipBounds.Width / m_Data.NumSamples;

            grfx.PageScale = m_PageScale;

            RectangleF visBounds = grfx.VisibleClipBounds;

            grfx.DrawLine( pen, 0, visBounds.Height / 2, visBounds.Width, visBounds.Height / 2 );

            grfx.TranslateTransform( 0, visBounds.Height );
            grfx.ScaleTransform( 1, -1 );

            Draw16Bit( grfx, pen, visBounds );

        }

        void Draw16Bit( Graphics grfx, Pen pen, RectangleF visBounds )
        {
            short val = m_Data[ 0 ];

            int prevX = 0;
            int prevY = (int) (( (val + 32768) * visBounds.Height ) / 65536 );

            for ( int i = 0; i < m_Data.NumSamples; i++ )
            {
                val = m_Data[ i ];

                int scaledVal = (int) (( (val + 32768) * visBounds.Height ) / 65536 );

                grfx.DrawLine( pen, prevX, prevY, i, scaledVal );

                prevX = i;
                prevY = scaledVal;

                if ( m_Fmt.Channels == 2 )
                    i++;
            }
        }

if code above is executed, the result is below...
formpaint.jpg

and the code below is executed.
  private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            
            Pen pen = new Pen(ForeColor);
            Graphics grh= Graphics.FromHwnd(this.Handle);
            Rectangle crec = this.ClientRectangle;

            System.Windows.Forms.PaintEventArgs pe= new PaintEventArgs(grh,crec);
            if (m_DrawWave)
            {
                sbpMainPanel.Text = "Drawing .WAV file...";

                wave.Draw(pe, pen);

                sbpMainPanel.Text = "Finished drawing .WAV file...";
            }
        }

the result is below.
seetingnew_graphics.jpg

the graphs have diffenent size. how should I code to get the same results?
 
Last edited by a moderator:
It looks like the issue is with scale so my first guess would be that ScaleTransform doesn't work with the result of Graphics.FromHwnd. Try commenting that line out and see if the results are the same to confirm or deny my guess.
 
Hello.
how could I make it work? when i comment out scaletransform(), nothing appears except the center line..any suggestions are welcome.

happy programming. cheers.
 
Last edited:
my project's entire codes.

have a look at my project's entire code and say something helpful.

my project is consisted of 2 c# files. I wanna display the wave graph whenever I click the form1. and the problem is that form1_paint event continues to repanit the graph. so I wanna show just once when I click the form1.




1. WaveFile.cs


C#:
[B]

using System;
[/B][B]using System.IO;[/B]
[B]using System.Drawing;[/B]
[B]using System.Windows.Forms;[/B]

[B]using System.Diagnostics;[/B]

[B]namespace AudioUtils[/B]
[B]{[/B]
[B]    /// <summary>[/B]
[B]    /// Summary description for WaveFile.[/B]
[B]    /// </summary>[/B]
[B]    public class WaveFile[/B]
[B]    {[/B]
[B]        /// <summary>[/B]
[B]        /// The Riff header is 12 bytes long[/B]
[B]        /// </summary>[/B]
[B]        class Riff[/B]
[B]        {[/B]
[B]            public Riff()[/B]
[B]            {[/B]
[B]                m_RiffID = new byte[ 4 ];[/B]
[B]                m_RiffFormat = new byte[ 4 ];[/B]
[B]            }[/B]

[B]            public void ReadRiff( FileStream inFS )[/B]
[B]            {[/B]
[B]                inFS.Read( m_RiffID, 0, 4 );[/B]
                
[B]                Debug.Assert( m_RiffID[0] == 82, "Riff ID Not Valid" );[/B]

[B]                BinaryReader binRead = new BinaryReader( inFS );[/B]

[B]                m_RiffSize = binRead.ReadUInt32( );[/B]

[B]                inFS.Read( m_RiffFormat, 0, 4 );[/B]
[B]            }[/B]

[B]            public byte[] RiffID[/B]
[B]            {[/B]
[B]                get { return m_RiffID; }[/B]
[B]            }[/B]

[B]            public uint RiffSize[/B]
[B]            {[/B]
[B]                get { return ( m_RiffSize ); }[/B]
[B]            }[/B]

[B]            public byte[] RiffFormat[/B]
[B]            {[/B]
[B]                get { return m_RiffFormat; }[/B]
[B]            }[/B]

[B]            private byte[]            m_RiffID;[/B]
[B]            private uint            m_RiffSize;[/B]
[B]            private byte[]            m_RiffFormat;[/B]
[B]        }[/B]

[B]        /// <summary>[/B]
[B]        /// The Format header is 24 bytes long[/B]
[B]        /// </summary>[/B]
[B]        class Fmt[/B]
[B]        {[/B]
[B]            public Fmt()[/B]
[B]            {[/B]
[B]                m_FmtID = new byte[ 4 ];[/B]
[B]            }[/B]

[B]            public void ReadFmt( FileStream inFS )[/B]
[B]            {[/B]
[B]                inFS.Read( m_FmtID, 0, 4 );[/B]

[B]                Debug.Assert( m_FmtID[0] == 102, "Format ID Not Valid" );[/B]

[B]                BinaryReader binRead = new BinaryReader( inFS );[/B]

[B]                m_FmtSize = binRead.ReadUInt32( );[/B]
[B]                m_FmtTag = binRead.ReadUInt16( );[/B]
[B]                m_Channels = binRead.ReadUInt16( );[/B]
[B]                m_SamplesPerSec = binRead.ReadUInt32( );[/B]
[B]                m_AverageBytesPerSec = binRead.ReadUInt32( );[/B]
[B]                m_BlockAlign = binRead.ReadUInt16( );[/B]
[B]                m_BitsPerSample = binRead.ReadUInt16( );[/B]

[B]                // This accounts for the variable format header size [/B]
[B]                // 12 bytes of Riff Header, 4 bytes for  FormatId, 4 bytes for FormatSize & the Actual size of the Format  Header [/B]
[B]                inFS.Seek( m_FmtSize + 20, System.IO.SeekOrigin.Begin );[/B]
[B]            }[/B]

[B]            public byte[] FmtID[/B]
[B]            {[/B]
[B]                get { return m_FmtID; }[/B]
[B]            }[/B]

[B]            public uint FmtSize[/B]
[B]            {[/B]
[B]                get { return m_FmtSize; }[/B]
[B]            }[/B]

[B]            public ushort FmtTag[/B]
[B]            {[/B]
[B]                get { return m_FmtTag; }[/B]
[B]            }[/B]

[B]            public ushort Channels[/B]
[B]            {[/B]
[B]                get { return m_Channels; }[/B]
[B]            }[/B]

[B]            public uint SamplesPerSec[/B]
[B]            {[/B]
[B]                get { return m_SamplesPerSec; }[/B]
[B]            }[/B]

[B]            public uint AverageBytesPerSec[/B]
[B]            {[/B]
[B]                get { return m_AverageBytesPerSec; }[/B]
[B]            }[/B]

[B]            public ushort BlockAlign[/B]
[B]            {[/B]
[B]                get { return m_BlockAlign; }[/B]
[B]            }[/B]

[B]            public ushort BitsPerSample[/B]
[B]            {[/B]
[B]                get { return m_BitsPerSample; }[/B]
[B]            }[/B]

[B]            private byte[]            m_FmtID;[/B]
[B]            private uint            m_FmtSize;[/B]
[B]            private ushort            m_FmtTag;[/B]
[B]            private ushort            m_Channels;[/B]
[B]            private uint            m_SamplesPerSec;[/B]
[B]            private uint            m_AverageBytesPerSec;[/B]
[B]            private ushort            m_BlockAlign;[/B]
[B]            private ushort            m_BitsPerSample;[/B]
[B]        }[/B]

[B]        /// <summary>[/B]
[B]        /// The Data block is 8 bytes + ???? long[/B]
[B]        /// </summary>[/B]
[B]        class Data[/B]
[B]        {[/B]
[B]            public Data()[/B]
[B]            {[/B]
[B]                m_DataID = new byte[ 4 ];[/B]
[B]            }[/B]

[B]            public void ReadData( FileStream inFS )[/B]
[B]            {[/B]
[B]                //inFS.Seek( 36, System.IO.SeekOrigin.Begin );[/B]

[B]                inFS.Read( m_DataID, 0, 4 );[/B]

[B]                Debug.Assert( m_DataID[0] == 100, "Data ID Not Valid" );[/B]

[B]                BinaryReader binRead = new BinaryReader( inFS );[/B]

[B]                m_DataSize = binRead.ReadUInt32( );[/B]

[B]                m_Data = new Int16[ m_DataSize ];[/B]

[B]                inFS.Seek( 40, System.IO.SeekOrigin.Begin );[/B]
            
[B]                m_NumSamples = (int) ( m_DataSize / 2 );[/B]

[B]                for ( int i = 0; i < m_NumSamples; i++)[/B]
[B]                {[/B]
[B]                    m_Data[ i ] = binRead.ReadInt16( );[/B]
[B]                }[/B]
[B]            } [/B]

[B]            public byte[] DataID[/B]
[B]            {[/B]
[B]                get { return m_DataID; }[/B]
[B]            }[/B]

[B]            public uint DataSize[/B]
[B]            {[/B]
[B]                get { return m_DataSize; }[/B]
[B]            }[/B]

[B]            public Int16 this[ int pos ][/B]
[B]            {[/B]
[B]                get { return m_Data[ pos ]; }[/B]
[B]            }[/B]

[B]            public int NumSamples[/B]
[B]            {[/B]
[B]                get { return m_NumSamples; }[/B]
[B]            }[/B]

[B]            private byte[]            m_DataID;[/B]
[B]            private uint            m_DataSize;[/B]
[B]            private Int16[]            m_Data;[/B]
[B]            private int                m_NumSamples;[/B]
[B]        }[/B]

[B]        public WaveFile( String inFilepath )[/B]
[B]        {[/B]
[B]            m_Filepath = inFilepath;[/B]
[B]            m_FileInfo = new FileInfo( inFilepath );[/B]
[B]            m_FileStream = m_FileInfo.OpenRead( );[/B]

[B]            m_Riff = new Riff( );[/B]
[B]            m_Fmt = new Fmt( );[/B]
[B]            m_Data = new Data( );[/B]
[B]        }[/B]

[B]        public void Read( )[/B]
[B]        {[/B]
[B]            m_Riff.ReadRiff( m_FileStream );[/B]
[B]            m_Fmt.ReadFmt( m_FileStream );[/B]
[B]            m_Data.ReadData( m_FileStream );[/B]
[B]        }[/B]

[B]        public void Draw( PaintEventArgs pea, Pen pen )[/B]
[B]        {[/B]
[B]            Graphics grfx = pea.Graphics;[/B]

[B]            if ( m_PageScale == 0.0f )[/B]
[B]                m_PageScale = grfx.VisibleClipBounds.Width / m_Data.NumSamples;[/B]

[B]            grfx.PageScale = m_PageScale;[/B]

[B]            RectangleF visBounds = grfx.VisibleClipBounds;[/B]

[B]            grfx.DrawLine( pen, 0, visBounds.Height / 2, visBounds.Width, visBounds.Height / 2 );[/B]

[B]            grfx.TranslateTransform( 0, visBounds.Height );[/B]
[B]            grfx.ScaleTransform( 1, -1 );[/B]

[B]            Draw16Bit( grfx, pen, visBounds );[/B]

[B]        }[/B]

[B]        void Draw16Bit( Graphics grfx, Pen pen, RectangleF visBounds )[/B]
[B]        {[/B]
[B]            short val = m_Data[ 0 ];[/B]

[B]            int prevX = 0;[/B]
[B]            int prevY = (int) (( (val + 32768) * visBounds.Height ) / 65536 );[/B]

[B]            for ( int i = 0; i < m_Data.NumSamples; i++ )[/B]
[B]            {[/B]
[B]                val = m_Data[ i ];[/B]

[B]                int scaledVal = (int) (( (val + 32768) * visBounds.Height ) / 65536 );[/B]

[B]                grfx.DrawLine( pen, prevX, prevY, i, scaledVal );[/B]

[B]                prevX = i;[/B]
[B]                prevY = scaledVal;[/B]

[B]                if ( m_Fmt.Channels == 2 )[/B]
[B]                    i++;[/B]
[B]            }[/B]
[B]        }[/B]

[B]        public void ZoomIn( )[/B]
[B]        {[/B]
[B]            m_PageScale /= 2;[/B]
[B]        }[/B]

[B]        public void ZoomOut( )[/B]
[B]        {[/B]
[B]            m_PageScale *= 2;[/B]
[B]        }[/B]

[B]        private string            m_Filepath;[/B]
[B]        private FileInfo        m_FileInfo;[/B]
[B]        private FileStream        m_FileStream;[/B]

[B]        private Riff            m_Riff;[/B]
[B]        private Fmt                m_Fmt;[/B]
[B]        private Data            m_Data;[/B]

[B]        private float            m_PageScale = 0.0f;[/B]

[B]    }[/B]
[B]}[/B]


2. Form1.cs
C#:
[B]using System;[/B]
[B]using System.Drawing;[/B]
[B]using System.Collections;[/B]
[B]using System.ComponentModel;[/B]
[B]using System.Windows.Forms;[/B]
[B]using System.Data;[/B]

[B]using AudioUtils;[/B]

[B]namespace ShowWaveForm[/B]
[B]{[/B]
[B]    /// <summary>[/B]
[B]    /// Summary description for Form1.[/B]
[B]    /// </summary>[/B]
[B]    public class Form1 : System.Windows.Forms.Form[/B]
[B]    {[/B]
[B]        private System.Windows.Forms.MainMenu mainMenu1;[/B]
[B]        private System.Windows.Forms.MenuItem file;[/B]
[B]        private System.Windows.Forms.MenuItem fileOpen;[/B]
[B]        private IContainer components;[/B]
        
[B]        WaveFile wave;[/B]
[B]        StatusBarPanel sbpMainPanel;[/B]
[B]        bool m_DrawWave = false;[/B]

[B]        public Form1()[/B]
[B]        {[/B]
[B]            //[/B]
[B]            // Required for Windows Form Designer support[/B]
[B]            //[/B]
[B]            InitializeComponent();[/B]
            
[B]            BackColor = SystemColors.Window;[/B]
[B]            ForeColor = SystemColors.WindowText;[/B]
[B]            ResizeRedraw = true;[/B]

[B]            StatusBar sb = new StatusBar();[/B]
[B]            sb.Parent = this;[/B]
[B]            sb.ShowPanels = true;[/B]
            
[B]            sbpMainPanel = new StatusBarPanel( );[/B]
[B]            sbpMainPanel.Text = "Ready";[/B]
[B]            sbpMainPanel.AutoSize = StatusBarPanelAutoSize.Spring;[/B]

[B]            sb.Panels.Add( sbpMainPanel );[/B]
[B]        }[/B]

[B]        /// <summary>[/B]
[B]        /// Clean up any resources being used.[/B]
[B]        /// </summary>[/B]
[B]        protected override void Dispose( bool disposing )[/B]
[B]        {[/B]
[B]            if( disposing )[/B]
[B]            {[/B]
[B]                if (components != null) [/B]
[B]                {[/B]
[B]                    components.Dispose();[/B]
[B]                }[/B]
[B]            }[/B]
[B]            base.Dispose( disposing );[/B]
[B]        }[/B]

[B]        #region Windows Form Designer generated code[/B]
[B]        /// <summary>[/B]
[B]        /// Required method for Designer support - do not modify[/B]
[B]        /// the contents of this method with the code editor.[/B]
[B]        /// </summary>[/B]
[B]        private void InitializeComponent()[/B]
[B]        {[/B]
[B]            this.components = new System.ComponentModel.Container();[/B]
[B]            this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);[/B]
[B]            this.file = new System.Windows.Forms.MenuItem();[/B]
[B]            this.fileOpen = new System.Windows.Forms.MenuItem();[/B]
[B]            this.SuspendLayout();[/B]
[B]            // [/B] 
[B]            // mainMenu1[/B]
[B]            // [/B] 
[B]            this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {[/B]
[B]            this.file});[/B]
[B]            // [/B] 
[B]            // file[/B]
[B]            // [/B] 
[B]            this.file.Index = 0;[/B]
[B]            this.file.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {[/B]
[B]            this.fileOpen});[/B]
[B]            this.file.Text = "&File";[/B]
[B]            // [/B] 
[B]            // fileOpen[/B]
[B]            // [/B] 
[B]            this.fileOpen.Index = 0;[/B]
[B]            this.fileOpen.Text = "&Open";[/B]
[B]            this.fileOpen.Click += new System.EventHandler(this.fileOpen_Click);[/B]
[B]            // [/B] 
[B]            // Form1[/B]
[B]            // [/B] 
[B]            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;[/B]
[B]            this.ClientSize = new System.Drawing.Size(896, 475);[/B]
[B]            this.Menu = this.mainMenu1;[/B]
[B]            this.Name = "Form1";[/B]
[B]            this.Text = "ShowWaveForm";[/B]
[B]            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);[/B]
[B]            this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);[/B]
[B]            this.ResumeLayout(false);[/B]

[B]        }[/B]
[B]        #endregion[/B]

[B]        /// <summary>[/B]
[B]        /// The main entry point for the application.[/B]
[B]        /// </summary>[/B]
[B]        [STAThread][/B]
[B]        static void Main() [/B]
[B]        {[/B]
[B]            Application.Run(new Form1());[/B]
[B]        }[/B]

[B]        private void fileOpen_Click(object sender, System.EventArgs e)[/B]
[B]        {[/B]
[B]            OpenFileDialog fileDlg = new OpenFileDialog();[/B]

[B]            if ( fileDlg.ShowDialog() == DialogResult.OK )[/B]
[B]            {[/B]
[B]                wave = new WaveFile( fileDlg.FileName );[/B]

[B]                sbpMainPanel.Text = "Reading .WAV file...";[/B]

[B]                wave.Read( );[/B]

[B]                sbpMainPanel.Text = "Finished Reading .WAV file...";[/B]

[B]                m_DrawWave = true;[/B]

[B]                Refresh( );[/B]

[B]            }[/B]
[B]        }[/B]

[B]        private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)[/B]
[B]        {[/B]
[B]            Pen pen = new Pen(ForeColor);[/B]
[B]            if (m_DrawWave)[/B]
[B]            {[/B]
[B]                sbpMainPanel.Text = "Drawing .WAV file...";[/B]

[B]                wave.Draw(e, pen);[/B]

[B]                sbpMainPanel.Text = "Finished drawing .WAV file...";[/B]
[B]            }[/B]
[B]        }[/B]

[B]        protected override void OnMouseWheel( MouseEventArgs mea )[/B]
[B]        {[/B]
[B]            if ( mea.Delta * SystemInformation.MouseWheelScrollLines / 120 > 0 )[/B]
[B]                wave.ZoomIn( );[/B]
[B]            else[/B]
[B]                wave.ZoomOut( );[/B]

[B]            Refresh( );[/B]
[B]        }[/B]

[B]        private void Form1_MouseClick(object sender, MouseEventArgs e)[/B]
[B]        {[/B]
            
[B]            Pen pen = new Pen(ForeColor);[/B]
[B]            Graphics grh= Graphics.FromHwnd(this.Handle);[/B]
[B]            Rectangle crec = this.ClientRectangle;[/B]

[B]            System.Windows.Forms.PaintEventArgs pe= new PaintEventArgs(grh,crec);[/B]
[B]            if (m_DrawWave)[/B]
[B]            {[/B]
[B]                sbpMainPanel.Text = "Drawing .WAV file...";[/B]

[B]                wave.Draw(pe, pen);[/B]

[B]                sbpMainPanel.Text = "Finished drawing .WAV file...";[/B]
[B]            }[/B]
[B]        }[/B]

[B]        //private void pictureBox1_Paint(object sender, PaintEventArgs e)[/B]
[B]        //{[/B]

[B]        //    //m_DrawWave = false;[/B]
[B]        //}[/B]

[B]    }[/B]
[B]}[/B]

the form has Mainmenu called File which has a submenu called open.
see that and let me know if you can work it out.
 
have a look at my project's entire code and say something helpful.

I normally wouldn't trawl through that much code regardless but not with that attitude.
 
oh, I was not polite to ask my issue. I guess. If you are offended by that. I am really sorry about it.

regards and happy programming.
 

Latest posts

Back
Top Bottom