Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Export JPEG with Flash/ASP.NET

0.00/5 (No votes)
28 Nov 2007 1  
How to export an jpeg image from Flash using ASP.NET

Introduction

The new class that ships with Flash 8 "flash.display.BitmapData" allow us to do some pretty interesting new things using Flash and PHP/ASP.NET. I recently found and article that shows you how use the BitmapData.gePixel() method export a screenshot from a flash movie and save it to the server.

As with most Flash examples there is little of no help for ASP.Net developers, as most of the examples use PHP. The code below is extention off the following article http://www.sephiroth.it/tutorials/flashPHP/print_screen/index.php and is basically just a translation of their PHP code to ASP.NET.

Using the code

If you follow the article then the code below should be pretty clear. I tried to keep the format and most of the code the same to make it easier to compare.

        int height = Convert.ToInt32(Request["height"]);
        int width = Convert.ToInt32(Request["width"]);
        // create the image with desired width and height

        Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
        Graphics g = Graphics.FromImage(bmp);
        // now fill the image with blank color

        // do you remember i wont pass the 0xFFFFFF pixels 

        // from flash?

        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 = Request["px" + 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("~/test.jpg"), ImageFormat.Jpeg);
        g.Dispose();
        bmp.Dispose();

Hope this helps somebody.

History

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here