[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 849
  • Last Modified:

converting PHP code to C#

will u please help me to convert php code to C#
my php code is

<?php
 $data = explode(",", $_POST['img']);
 $width = $_POST['width'];
 $height = $_POST['height'];
 $image=imagecreatetruecolor( $width ,$height );
 $background = imagecolorallocate( $image ,0 , 0 , 0 );
 //Copy pixels
 $i = 0;
 for($x=0; $x<=$width; $x++){
  for($y=0; $y<=$height; $y++){
   $int = hexdec($data[$i++]);
   $color = ImageColorAllocate ($image, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
   imagesetpixel ( $image , $x , $y , $color );
  }
 }
 //Output image and clean
 //header( "Content-type: image/jpeg" );
 imagejpeg($image,"files/new.jpg");
 //imagedestroy( $image );
?>
0
ninusajil
Asked:
ninusajil
  • 2
1 Solution
 
Haris VCommented:
0
 
ninusajilAuthor Commented:
yes, i tried but that is asking to use .net 1 frame work iam using 2.0
0
 
ninusajilAuthor Commented:
i got it as

 int height = Convert.ToInt32(50);
            int width = Convert.ToInt32(50);
            // create the image with desired width and height
            StreamReader objReader = default(StreamReader);
            objReader = File.OpenText(Server.MapPath("data.txt"));
            string pixel = objReader.ReadToEnd();
            Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            Graphics g = Graphics.FromImage(bmp);
            // now fill the image with blank color
            g.FillRectangle(new SolidBrush(Color.White), 0, 0, bmp.Width, bmp.Height);
            // now process every POST variable which

            // contains a pixel color

            for (int rows = 0; rows < height; rows++)
            {
                // convert the string into an array of n elements

                string[] row = (pixel + rows).ToString().Split(",".ToCharArray());
                for (int cols = 0; cols < width; cols++)
                {
                    // get the single pixel color value

                    string value = row[cols];
                    // if value is not empty (empty values are the blank pixels)

                    if (value != "")
                    {
                        // get the hexadecimal string (must be 6 chars length)

                        // so add the missing chars if needed

                        value = value.PadLeft(6, Convert.ToChar("0"));
                        // Convert the hex code color string to a standard color class.

                        int R = Convert.ToInt32(value.Substring(0, 2), 16);
                        int G = Convert.ToInt32(value.Substring(2, 2), 16);
                        int B = Convert.ToInt32(value.Substring(4, 2), 16);
                        System.Drawing.Color color = Color.FromArgb(R, G, B);
                        g.FillRegion(new SolidBrush(color), new Region(new Rectangle(cols, rows, 1, 1)));
                    }
                }
            }
            bmp.Save(Server.MapPath("~/PixelImage/test.jpg"), ImageFormat.Jpeg);
            g.Dispose();
            bmp.Dispose();
            objReader.Close();
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now