Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. making a white black smooth animation

making a white black smooth animation

Scheduled Pinned Locked Moved C#
graphics
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sajjad Izadi
    wrote on last edited by
    #1

    hi friends and excuse for this long post, i got some idea from greeeg about making a picture white and black. the method was:

    public static bool GrayScale(Bitmap b)
    {
    BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

    int stride = bmData.Stride;
    System.IntPtr Scan0 = bmData.Scan0;
    
    unsafe
    {
               	//using a pointer to point to the colors of picture
    	byte \* p = (byte \*)(void \*)Scan0;
    
    	int nOffset = stride - b.Width\*3;
    
    	byte red, green, blue;
            
    	for(int y=0;y
    	{
    		for(int x=0; x < b.Width; ++x )
    		{
    			blue = p\[0\];
    			green = p\[1\];
    			red = p\[2\];
    
    			//changing the colors
    			p\[0\] = p\[1\] = p\[2\] = (byte)(.299 \* red + .587 \* green + .114 \* blue);
    
    			p += 3;
    		}
    		p += nOffset;
    	}
    }
    
    b.UnlockBits(bmData);
    return true;
    

    }

    it takes a picture as the argument and processes it to make it white and black. then, again greeeg helped me and gave a method to make picture become white and black in a defined timings (like the smooth conversion of colored screen to grayscale when you click 'Turn Off' in strat menu in windows xp):

    const float transitionTime = 5000f; // 5 seconds
    float normalizedTransitionTime = millsecondsSinceTransitionStart / transitionTime;

    red = (byte)Lerp(normalizedTransitionTime , coloredPixelR, bwPixelR);
    green = (byte)Lerp(normalizedTransitionTime , coloredPixelG, bwPixelG);
    blue = (byte)Lerp(normalizedTransitionTime , coloredPixelB, bwPixelB);

    and said that the Lerp method is actually works like this:

    float Lerp(float amount, float value1, float value2)
    {
    return value1 + (value2 - value1) * amount;
    }

    for conversion of a picture from colored state to gray scale in a time like 500 milliseconds i merged the above ideas (of greeeg). so i changed the first 'GrayScale(Bitmap b)' method to this:

    public static bool GrayScale(Bitmap b)
    {
    BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

    int stride = bmData.Stride;
    IntPtr Scan0 = bmData.Scan0;
    
    unsafe
    {
    	byte \* p = (byte \*)(void \*)Scan0;
    
    	int nOffset = stride - b.Width\*3
                  byte red, green, blue;
                
    	for(int y=0;y
    	{
    		for(int x=0; x < b.Width; ++x )
    		{
    			blue = p\[0\];
    			green = p\[1\];
    			red = p\[2\];
    
    		//my changings start h
    
    C 1 Reply Last reply
    0
    • S Sajjad Izadi

      hi friends and excuse for this long post, i got some idea from greeeg about making a picture white and black. the method was:

      public static bool GrayScale(Bitmap b)
      {
      BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

      int stride = bmData.Stride;
      System.IntPtr Scan0 = bmData.Scan0;
      
      unsafe
      {
                 	//using a pointer to point to the colors of picture
      	byte \* p = (byte \*)(void \*)Scan0;
      
      	int nOffset = stride - b.Width\*3;
      
      	byte red, green, blue;
              
      	for(int y=0;y
      	{
      		for(int x=0; x < b.Width; ++x )
      		{
      			blue = p\[0\];
      			green = p\[1\];
      			red = p\[2\];
      
      			//changing the colors
      			p\[0\] = p\[1\] = p\[2\] = (byte)(.299 \* red + .587 \* green + .114 \* blue);
      
      			p += 3;
      		}
      		p += nOffset;
      	}
      }
      
      b.UnlockBits(bmData);
      return true;
      

      }

      it takes a picture as the argument and processes it to make it white and black. then, again greeeg helped me and gave a method to make picture become white and black in a defined timings (like the smooth conversion of colored screen to grayscale when you click 'Turn Off' in strat menu in windows xp):

      const float transitionTime = 5000f; // 5 seconds
      float normalizedTransitionTime = millsecondsSinceTransitionStart / transitionTime;

      red = (byte)Lerp(normalizedTransitionTime , coloredPixelR, bwPixelR);
      green = (byte)Lerp(normalizedTransitionTime , coloredPixelG, bwPixelG);
      blue = (byte)Lerp(normalizedTransitionTime , coloredPixelB, bwPixelB);

      and said that the Lerp method is actually works like this:

      float Lerp(float amount, float value1, float value2)
      {
      return value1 + (value2 - value1) * amount;
      }

      for conversion of a picture from colored state to gray scale in a time like 500 milliseconds i merged the above ideas (of greeeg). so i changed the first 'GrayScale(Bitmap b)' method to this:

      public static bool GrayScale(Bitmap b)
      {
      BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

      int stride = bmData.Stride;
      IntPtr Scan0 = bmData.Scan0;
      
      unsafe
      {
      	byte \* p = (byte \*)(void \*)Scan0;
      
      	int nOffset = stride - b.Width\*3
                    byte red, green, blue;
                  
      	for(int y=0;y
      	{
      		for(int x=0; x < b.Width; ++x )
      		{
      			blue = p\[0\];
      			green = p\[1\];
      			red = p\[2\];
      
      		//my changings start h
      
      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Sajjad Izadi wrote:

      can i change Red, Green and Blue colors of picture (to make it gray scale) in an other way (without using the pointers

      Yes, but it would be much slower. This is the fastest way to do it. Sounds like someone else has done all the work for you so far. I'm not sure what you're asking exactly, 'the result was unsuccessful' doesn't tell us much.

      Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      S 1 Reply Last reply
      0
      • C Christian Graus

        Sajjad Izadi wrote:

        can i change Red, Green and Blue colors of picture (to make it gray scale) in an other way (without using the pointers

        Yes, but it would be much slower. This is the fastest way to do it. Sounds like someone else has done all the work for you so far. I'm not sure what you're asking exactly, 'the result was unsuccessful' doesn't tell us much.

        Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        S Offline
        S Offline
        Sajjad Izadi
        wrote on last edited by
        #3

        i don't know how to use these two methods to make a smooth conversion of colored picture to a gray scale one in a ditinct time. yes, i myself never could guess to use such pointers to do my work. i'm learning from the friends.

        C 1 Reply Last reply
        0
        • S Sajjad Izadi

          i don't know how to use these two methods to make a smooth conversion of colored picture to a gray scale one in a ditinct time. yes, i myself never could guess to use such pointers to do my work. i'm learning from the friends.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Well, not sure why I would help you if I get a 1 vote for my trouble, but, the fact is, for bigger images, you probably can't. You'd need to create the individual frames first, THEN roll through them, for it to be smooth.

          Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

          S 1 Reply Last reply
          0
          • C Christian Graus

            Well, not sure why I would help you if I get a 1 vote for my trouble, but, the fact is, for bigger images, you probably can't. You'd need to create the individual frames first, THEN roll through them, for it to be smooth.

            Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

            S Offline
            S Offline
            Sajjad Izadi
            wrote on last edited by
            #5

            you mean to make the frames as an *.jpg in a folder and start to play them in my form like an animation. so the new and i think main question is "how is making a half grayed picture to produce the frames"? i mean a picture which is not completely colord nor completely gray.

            modified on Monday, June 9, 2008 2:04 PM

            C 1 Reply Last reply
            0
            • S Sajjad Izadi

              you mean to make the frames as an *.jpg in a folder and start to play them in my form like an animation. so the new and i think main question is "how is making a half grayed picture to produce the frames"? i mean a picture which is not completely colord nor completely gray.

              modified on Monday, June 9, 2008 2:04 PM

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              Actually, an easier way to do all this. Create your grey image. Then draw it on top of the color one over and over with varying transparency, which you get by setting the opacity of the ImageAttributes class.

              Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups