C# Animation and music at the same time












0














I write the program in the console, at startup, first play the music, and then the animation. Help make animation and music at the same time. Rummaged through the Internet and found nothing



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.Media;
using System.Threading;

namespace Animation_and_music
{
class animation
{
public void music()
{
SoundPlayer player = new SoundPlayer("C:\audio.wav");
player.PlaySync();
}
public void gif()
{
Console.SetWindowSize(102, 49);
Image image = Image.FromFile(@"1.gif");
FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);
int frameCount = image.GetFrameCount(dimension);
StringBuilder sb;

int left = Console.WindowLeft, top = Console.WindowTop;

char chars = { '#', '#', '@', '%', '=', '+', '*', ':', '-', '.', ' ' };

for (int i = 0; ; i = (i + 1) % frameCount)
{
sb = new StringBuilder();
image.SelectActiveFrame(dimension, i);

for (int h = 0; h < image.Height; h++)
{
for (int w = 0; w < image.Width; w++)
{
Color cl = ((Bitmap)image).GetPixel(w, h);
int gray = (cl.R + cl.R + cl.B) / 3;
int index = (gray * (chars.Length - 1)) / 255;

sb.Append(chars[index]);
}
sb.Append('n');
}

Console.SetCursorPosition(left, top);
Console.Write(sb.ToString());

System.Threading.Thread.Sleep(50);
}
}

static Image ScaleImage(Image source, int width, int height)
{
Image dest = new Bitmap(width, height);
using (Graphics gr = Graphics.FromImage(dest))
{
gr.FillRectangle(Brushes.White, 0, 0, width, height);
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

float srcwidth = source.Width;
float srcheight = source.Height;
float dstwidth = width;
float dstheight = height;

if (srcwidth <= dstwidth && srcheight <= dstheight)
{
int left = (width - source.Width) / 2;
int top = (height - source.Height) / 2;
gr.DrawImage(source, left, top, source.Width, source.Height);
}
else if (srcwidth / srcheight > dstwidth * dstheight)
{
float cy = srcheight / srcwidth * dstwidth;
float top = ((float)dstheight - cy) / 2.0f;
if (top < 1.0f) top = 0;
gr.DrawImage(source, 0, top, dstwidth, cy);
}
else
{
float cx = srcwidth / srcheight * dstheight;
float left = ((float)dstwidth - cx) / 2.0f;
if (left < 1.0f) left = 0;
gr.DrawImage(source, 0, left, cx, dstheight);
}
return dest;
}
}
}
}


"static void Main(string args)" is in another code



Thanks in advance for your help
(Sorry for my english, I use a translator)










share|improve this question
























  • Try player.Play(); in your music method instead of player.PlaySync();. If this fixes your problem I'll be happy to post an answer explaining why
    – MindSwipe
    Nov 16 at 10:03


















0














I write the program in the console, at startup, first play the music, and then the animation. Help make animation and music at the same time. Rummaged through the Internet and found nothing



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.Media;
using System.Threading;

namespace Animation_and_music
{
class animation
{
public void music()
{
SoundPlayer player = new SoundPlayer("C:\audio.wav");
player.PlaySync();
}
public void gif()
{
Console.SetWindowSize(102, 49);
Image image = Image.FromFile(@"1.gif");
FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);
int frameCount = image.GetFrameCount(dimension);
StringBuilder sb;

int left = Console.WindowLeft, top = Console.WindowTop;

char chars = { '#', '#', '@', '%', '=', '+', '*', ':', '-', '.', ' ' };

for (int i = 0; ; i = (i + 1) % frameCount)
{
sb = new StringBuilder();
image.SelectActiveFrame(dimension, i);

for (int h = 0; h < image.Height; h++)
{
for (int w = 0; w < image.Width; w++)
{
Color cl = ((Bitmap)image).GetPixel(w, h);
int gray = (cl.R + cl.R + cl.B) / 3;
int index = (gray * (chars.Length - 1)) / 255;

sb.Append(chars[index]);
}
sb.Append('n');
}

Console.SetCursorPosition(left, top);
Console.Write(sb.ToString());

System.Threading.Thread.Sleep(50);
}
}

static Image ScaleImage(Image source, int width, int height)
{
Image dest = new Bitmap(width, height);
using (Graphics gr = Graphics.FromImage(dest))
{
gr.FillRectangle(Brushes.White, 0, 0, width, height);
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

float srcwidth = source.Width;
float srcheight = source.Height;
float dstwidth = width;
float dstheight = height;

if (srcwidth <= dstwidth && srcheight <= dstheight)
{
int left = (width - source.Width) / 2;
int top = (height - source.Height) / 2;
gr.DrawImage(source, left, top, source.Width, source.Height);
}
else if (srcwidth / srcheight > dstwidth * dstheight)
{
float cy = srcheight / srcwidth * dstwidth;
float top = ((float)dstheight - cy) / 2.0f;
if (top < 1.0f) top = 0;
gr.DrawImage(source, 0, top, dstwidth, cy);
}
else
{
float cx = srcwidth / srcheight * dstheight;
float left = ((float)dstwidth - cx) / 2.0f;
if (left < 1.0f) left = 0;
gr.DrawImage(source, 0, left, cx, dstheight);
}
return dest;
}
}
}
}


"static void Main(string args)" is in another code



Thanks in advance for your help
(Sorry for my english, I use a translator)










share|improve this question
























  • Try player.Play(); in your music method instead of player.PlaySync();. If this fixes your problem I'll be happy to post an answer explaining why
    – MindSwipe
    Nov 16 at 10:03
















0












0








0


2





I write the program in the console, at startup, first play the music, and then the animation. Help make animation and music at the same time. Rummaged through the Internet and found nothing



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.Media;
using System.Threading;

namespace Animation_and_music
{
class animation
{
public void music()
{
SoundPlayer player = new SoundPlayer("C:\audio.wav");
player.PlaySync();
}
public void gif()
{
Console.SetWindowSize(102, 49);
Image image = Image.FromFile(@"1.gif");
FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);
int frameCount = image.GetFrameCount(dimension);
StringBuilder sb;

int left = Console.WindowLeft, top = Console.WindowTop;

char chars = { '#', '#', '@', '%', '=', '+', '*', ':', '-', '.', ' ' };

for (int i = 0; ; i = (i + 1) % frameCount)
{
sb = new StringBuilder();
image.SelectActiveFrame(dimension, i);

for (int h = 0; h < image.Height; h++)
{
for (int w = 0; w < image.Width; w++)
{
Color cl = ((Bitmap)image).GetPixel(w, h);
int gray = (cl.R + cl.R + cl.B) / 3;
int index = (gray * (chars.Length - 1)) / 255;

sb.Append(chars[index]);
}
sb.Append('n');
}

Console.SetCursorPosition(left, top);
Console.Write(sb.ToString());

System.Threading.Thread.Sleep(50);
}
}

static Image ScaleImage(Image source, int width, int height)
{
Image dest = new Bitmap(width, height);
using (Graphics gr = Graphics.FromImage(dest))
{
gr.FillRectangle(Brushes.White, 0, 0, width, height);
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

float srcwidth = source.Width;
float srcheight = source.Height;
float dstwidth = width;
float dstheight = height;

if (srcwidth <= dstwidth && srcheight <= dstheight)
{
int left = (width - source.Width) / 2;
int top = (height - source.Height) / 2;
gr.DrawImage(source, left, top, source.Width, source.Height);
}
else if (srcwidth / srcheight > dstwidth * dstheight)
{
float cy = srcheight / srcwidth * dstwidth;
float top = ((float)dstheight - cy) / 2.0f;
if (top < 1.0f) top = 0;
gr.DrawImage(source, 0, top, dstwidth, cy);
}
else
{
float cx = srcwidth / srcheight * dstheight;
float left = ((float)dstwidth - cx) / 2.0f;
if (left < 1.0f) left = 0;
gr.DrawImage(source, 0, left, cx, dstheight);
}
return dest;
}
}
}
}


"static void Main(string args)" is in another code



Thanks in advance for your help
(Sorry for my english, I use a translator)










share|improve this question















I write the program in the console, at startup, first play the music, and then the animation. Help make animation and music at the same time. Rummaged through the Internet and found nothing



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.Media;
using System.Threading;

namespace Animation_and_music
{
class animation
{
public void music()
{
SoundPlayer player = new SoundPlayer("C:\audio.wav");
player.PlaySync();
}
public void gif()
{
Console.SetWindowSize(102, 49);
Image image = Image.FromFile(@"1.gif");
FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);
int frameCount = image.GetFrameCount(dimension);
StringBuilder sb;

int left = Console.WindowLeft, top = Console.WindowTop;

char chars = { '#', '#', '@', '%', '=', '+', '*', ':', '-', '.', ' ' };

for (int i = 0; ; i = (i + 1) % frameCount)
{
sb = new StringBuilder();
image.SelectActiveFrame(dimension, i);

for (int h = 0; h < image.Height; h++)
{
for (int w = 0; w < image.Width; w++)
{
Color cl = ((Bitmap)image).GetPixel(w, h);
int gray = (cl.R + cl.R + cl.B) / 3;
int index = (gray * (chars.Length - 1)) / 255;

sb.Append(chars[index]);
}
sb.Append('n');
}

Console.SetCursorPosition(left, top);
Console.Write(sb.ToString());

System.Threading.Thread.Sleep(50);
}
}

static Image ScaleImage(Image source, int width, int height)
{
Image dest = new Bitmap(width, height);
using (Graphics gr = Graphics.FromImage(dest))
{
gr.FillRectangle(Brushes.White, 0, 0, width, height);
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

float srcwidth = source.Width;
float srcheight = source.Height;
float dstwidth = width;
float dstheight = height;

if (srcwidth <= dstwidth && srcheight <= dstheight)
{
int left = (width - source.Width) / 2;
int top = (height - source.Height) / 2;
gr.DrawImage(source, left, top, source.Width, source.Height);
}
else if (srcwidth / srcheight > dstwidth * dstheight)
{
float cy = srcheight / srcwidth * dstwidth;
float top = ((float)dstheight - cy) / 2.0f;
if (top < 1.0f) top = 0;
gr.DrawImage(source, 0, top, dstwidth, cy);
}
else
{
float cx = srcwidth / srcheight * dstheight;
float left = ((float)dstwidth - cx) / 2.0f;
if (left < 1.0f) left = 0;
gr.DrawImage(source, 0, left, cx, dstheight);
}
return dest;
}
}
}
}


"static void Main(string args)" is in another code



Thanks in advance for your help
(Sorry for my english, I use a translator)







c# animation audio-player






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Stephen Kennedy

7,079134867




7,079134867










asked Nov 16 at 10:02









H0d3ine

63




63












  • Try player.Play(); in your music method instead of player.PlaySync();. If this fixes your problem I'll be happy to post an answer explaining why
    – MindSwipe
    Nov 16 at 10:03




















  • Try player.Play(); in your music method instead of player.PlaySync();. If this fixes your problem I'll be happy to post an answer explaining why
    – MindSwipe
    Nov 16 at 10:03


















Try player.Play(); in your music method instead of player.PlaySync();. If this fixes your problem I'll be happy to post an answer explaining why
– MindSwipe
Nov 16 at 10:03






Try player.Play(); in your music method instead of player.PlaySync();. If this fixes your problem I'll be happy to post an answer explaining why
– MindSwipe
Nov 16 at 10:03














2 Answers
2






active

oldest

votes


















3














Your problem is that your trying to do two things at the same time in only one thread, which is impossible, one thread can only do one thing at a time. You need to use multiple threads to achieve your goal, which in this case is simple enough just use player.Play(); instead of player.PlaySync();.
player.Play(); automatically starts a new thread and runs the task there.



Here is a good tutorial/ introduction to threads in C#






share|improve this answer





























    0














    As @MindSwipe said you need to use more threads.



    Example:



    class Animation
    {
    public Animation
    {
    Thread mythread = new Thread(DoSomething);
    Thread mythread2 = new Thread(DoSomething2);
    mythread.Start();
    mythread2.Start();
    }

    public void DoSomething()
    {
    Do_gif_animation();
    }
    public void DoSomething2()
    {
    Do_music();
    }

    }





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335469%2fc-sharp-animation-and-music-at-the-same-time%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      Your problem is that your trying to do two things at the same time in only one thread, which is impossible, one thread can only do one thing at a time. You need to use multiple threads to achieve your goal, which in this case is simple enough just use player.Play(); instead of player.PlaySync();.
      player.Play(); automatically starts a new thread and runs the task there.



      Here is a good tutorial/ introduction to threads in C#






      share|improve this answer


























        3














        Your problem is that your trying to do two things at the same time in only one thread, which is impossible, one thread can only do one thing at a time. You need to use multiple threads to achieve your goal, which in this case is simple enough just use player.Play(); instead of player.PlaySync();.
        player.Play(); automatically starts a new thread and runs the task there.



        Here is a good tutorial/ introduction to threads in C#






        share|improve this answer
























          3












          3








          3






          Your problem is that your trying to do two things at the same time in only one thread, which is impossible, one thread can only do one thing at a time. You need to use multiple threads to achieve your goal, which in this case is simple enough just use player.Play(); instead of player.PlaySync();.
          player.Play(); automatically starts a new thread and runs the task there.



          Here is a good tutorial/ introduction to threads in C#






          share|improve this answer












          Your problem is that your trying to do two things at the same time in only one thread, which is impossible, one thread can only do one thing at a time. You need to use multiple threads to achieve your goal, which in this case is simple enough just use player.Play(); instead of player.PlaySync();.
          player.Play(); automatically starts a new thread and runs the task there.



          Here is a good tutorial/ introduction to threads in C#







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 at 10:25









          MindSwipe

          54313




          54313

























              0














              As @MindSwipe said you need to use more threads.



              Example:



              class Animation
              {
              public Animation
              {
              Thread mythread = new Thread(DoSomething);
              Thread mythread2 = new Thread(DoSomething2);
              mythread.Start();
              mythread2.Start();
              }

              public void DoSomething()
              {
              Do_gif_animation();
              }
              public void DoSomething2()
              {
              Do_music();
              }

              }





              share|improve this answer


























                0














                As @MindSwipe said you need to use more threads.



                Example:



                class Animation
                {
                public Animation
                {
                Thread mythread = new Thread(DoSomething);
                Thread mythread2 = new Thread(DoSomething2);
                mythread.Start();
                mythread2.Start();
                }

                public void DoSomething()
                {
                Do_gif_animation();
                }
                public void DoSomething2()
                {
                Do_music();
                }

                }





                share|improve this answer
























                  0












                  0








                  0






                  As @MindSwipe said you need to use more threads.



                  Example:



                  class Animation
                  {
                  public Animation
                  {
                  Thread mythread = new Thread(DoSomething);
                  Thread mythread2 = new Thread(DoSomething2);
                  mythread.Start();
                  mythread2.Start();
                  }

                  public void DoSomething()
                  {
                  Do_gif_animation();
                  }
                  public void DoSomething2()
                  {
                  Do_music();
                  }

                  }





                  share|improve this answer












                  As @MindSwipe said you need to use more threads.



                  Example:



                  class Animation
                  {
                  public Animation
                  {
                  Thread mythread = new Thread(DoSomething);
                  Thread mythread2 = new Thread(DoSomething2);
                  mythread.Start();
                  mythread2.Start();
                  }

                  public void DoSomething()
                  {
                  Do_gif_animation();
                  }
                  public void DoSomething2()
                  {
                  Do_music();
                  }

                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 at 12:38









                  matteo cracco

                  1019




                  1019






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335469%2fc-sharp-animation-and-music-at-the-same-time%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to change which sound is reproduced for terminal bell?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?