C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Become a C# Expert!

Join 306,756 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,821 people online right now. Registration is fast and FREE... Join Now!





Draw a Rectangular Prism(3D Rectangle)

Draw a rectangular prism. Control the direction the 3D object is positioned and specify three colors for the sides.

Submitted By: gbertoli3
Actions:
Rating:
Views: 351

Language: C#

Last Modified: October 30, 2009
Instructions: Add a reference to: System.Drawing and System.Drawing.Drawing2D

Use:
Graphics graphics = pictureBox1.CreateGraphics();
pictureBox1.Image = DrawRectangularPrism(pictureBox1.CreateGraphics(), Color.Red, Color.Green, Color.Blue, 125, 65, Direction3D.DiagonalDownLeft);

Snippet


  1.         /// <summary>
  2.         /// Specifies the direction the 3D object will be drawn.
  3.         /// </summary>
  4.         public enum Direction3D
  5.         {
  6.             /// <summary>
  7.             /// The cube is angled to the lower left.
  8.             /// </summary>
  9.             DiagonalDownLeft,
  10.             /// <summary>
  11.             /// The cube is angled to the lower right.
  12.             /// </summary>
  13.             DiagonalDownRight,
  14.             /// <summary>
  15.             /// The cube is angled to the upper left.
  16.             /// </summary>
  17.             DiagonalUpLeft,
  18.             /// <summary>
  19.             /// The cube is angled to the upper right.
  20.             /// </summary>
  21.             DiagonalUpRight
  22.         }
  23.  
  24.         /// <summary>
  25.         /// Draws a Rectangluar Prism, then returns it as a Bitmap.
  26.         /// </summary>
  27.         /// <param name="g">The graphics to use.</param>
  28.         /// <param name="frontColor">The color of the front of the rectangular prism.</param>
  29.         /// <param name="leftRightColor">The color on the left / right (depending on Direction3D) of the rectangular prism.</param>
  30.         /// <param name="topBottomColor">The color on the top / bottom (depending on Direction3D) of the rectangular prism.</param>
  31.         /// <param name="width">The width of the rectangular prism.</param>
  32.         /// <param name="height">The height of the rectangular prism.</param>
  33.         /// <param name="direction">The direction / angle to the rectangular prism will be drawn.</param>
  34.         /// <returns></returns>
  35.         public Bitmap DrawRectangularPrism(Graphics g, Color frontColor, Color leftRightColor, Color topBottomColor, int width, int height, Direction3D direction)
  36.         {
  37.             //The new width of the image
  38.             int newWidth = width + (width / 2);
  39.             //The new height of the image
  40.             int newHeight = height + (height / 2);
  41.             //Create a new bitmap with the new dimensions
  42.             Bitmap bitmap = new Bitmap(newWidth, newHeight, g);
  43.             //Create a new instance of the Graphics class from our new bitmap
  44.             Graphics graphics = Graphics.FromImage(bitmap);
  45.  
  46.             //Ensure that we use high quality
  47.             graphics.CompositingQuality = CompositingQuality.HighQuality;
  48.             graphics.InterpolationMode = InterpolationMode.High;
  49.             graphics.PageUnit = GraphicsUnit.Pixel;
  50.             graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
  51.             graphics.SmoothingMode = SmoothingMode.HighQuality;
  52.  
  53.             //A rectangle that represents the back of the rectangular prism
  54.             Rectangle back = new Rectangle();
  55.             //A rectangle that represents the front of the rectangular prism
  56.             Rectangle front = new Rectangle();
  57.             //Points that represent the top / bottom (depending on Direction3D) of the rectangular prism
  58.             Point[] top = new Point[] { };
  59.             //Points that represent the right / left side (depending on Direction3D) of the rectangular prism
  60.             Point[] right = new Point[] { };
  61.  
  62.             //Assign the proper values to back, front, top, and right depending on the direction chosen
  63.             switch (direction)
  64.             {
  65.                 //Assign values to make the angled towards the lower left
  66.                 case Direction3D.DiagonalDownLeft:
  67.                     {
  68.                         back = new Rectangle(width / 2, 1, width, height);
  69.                         front = new Rectangle(1, height / 2, width, height);
  70.                         top = new Point[] { new Point(1, height / 2), new Point(width / 2, 1), new Point(width + (width / 2), 1), new Point(width, height / 2) };
  71.                         right = new Point[] { new Point(width, height / 2), new Point(width + (width / 2), 1), new Point(width + (width / 2), height), new Point(width, height + (height / 2)) };
  72.                         break;
  73.                     }
  74.                 //Assign values to make the angled towards the lower right
  75.                 case Direction3D.DiagonalDownRight:
  76.                     {
  77.                         back = new Rectangle(1, 1, width, height);
  78.                         front = new Rectangle(width / 2, height / 2, width, height);
  79.                         top = new Point[] { new Point((width / 2), height / 2), new Point(1, 1), new Point(width, 1), new Point(width + (width / 2), height / 2) };
  80.                         right = new Point[] { new Point(1, height), new Point(1, 1), new Point(width / 2, height / 2), new Point(width / 2, height + (height / 2)) };
  81.                         break;
  82.                     }
  83.                 //Assign values to make the angled towards the upper left
  84.                 case Direction3D.DiagonalUpLeft:
  85.                     {
  86.                         back = new Rectangle(width / 2, height / 2, width, height);
  87.                         front = new Rectangle(1, 1, width, height);
  88.                         top = new Point[] { new Point(width / 2, height + (height / 2)), new Point(1, height), new Point(width, height), new Point(width + (width / 2), height + (height / 2)) };
  89.                         right = new Point[] { new Point(width, height), new Point(width, 1), new Point(width + (width / 2), height / 2), new Point(width + (width / 2), height + (height / 2)) };
  90.                         break;
  91.                     }
  92.                 //Assign values to make the angled towards the upper right
  93.                 case Direction3D.DiagonalUpRight:
  94.                     {
  95.                         back = new Rectangle(1, height / 2, width, height);
  96.                         front = new Rectangle(width / 2, 1, width, height);
  97.                         top = new Point[] { new Point(1, height + (height / 2)), new Point(width / 2, height), new Point(width + (width / 2), height), new Point(width, height + (height / 2)) };
  98.                         right = new Point[] { new Point(1, height + (height / 2)), new Point(1, height / 2), new Point(width / 2, 1), new Point(width / 2, height) };
  99.                         break;
  100.                     }
  101.             }
  102.  
  103.             //Fill the back with black(this will not be seen)
  104.             graphics.FillRectangle(new SolidBrush(Color.Black), back);
  105.             //Fill the front with our frontColor
  106.             graphics.FillRectangle(new SolidBrush(frontColor), front);
  107.             //Fill the top / bottom (depending on Direction3D) with our topBottomColor
  108.             graphics.FillPolygon(new SolidBrush(topBottomColor), top);
  109.             //Fill the left / right (depending on Direction3D) with our leftRightColor
  110.             graphics.FillPolygon(new SolidBrush(leftRightColor), right);
  111.  
  112.             //Free up some resources
  113.             graphics.Dispose();
  114.  
  115.             //Return the bitmap
  116.             return bitmap;
  117.         }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month