Solved

converting PHP code to C#

Posted on 2009-05-06
3
806 Views
Last Modified: 2012-05-06
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
Comment
Question by:ninusajil
  • 2
3 Comments
 
LVL 8

Expert Comment

by:Haris V
ID: 24322428
0
 

Author Comment

by:ninusajil
ID: 24322451
yes, i tried but that is asking to use .net 1 frame work iam using 2.0
0
 

Accepted Solution

by:
ninusajil earned 0 total points
ID: 24323559
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now