Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Code Example: ASP.NET Buttons (Gif) with Transparent background using GDI+
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Eric Johnson  
View profile  
 More options May 4 2003, 6:12 am
Newsgroups: microsoft.public.dotnet.framework.drawing
From: Eric.John...@sts.siemens.com (Eric Johnson)
Date: 4 May 2003 03:12:28 -0700
Local: Sun, May 4 2003 6:12 am
Subject: Code Example: ASP.NET Buttons (Gif) with Transparent background using GDI+
It took me quite a while to figure this out so I figured I would post
my code out for all my fellow NG users to view.  I would like to
figure out a way to make the buttons look better, but for now I'm kind
of tired of working on it.

If anyone is interested I also have a few other Web Form classes such
as TextToImage, RoundedCorner, and BackgroundGradient.  All of my
classes use transparent backgrounds are are configured through
querystring parms.  Eventually, I plan on publishing a few GDI+
articles, but for now hopefully this code will help someone out.

using System;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.IO;
using System.ComponentModel;
using System.Collections;

namespace DynamicWebDesign
{
        /// <summary>
        /// Summary description for TextButton.
        /// </summary>
        public class TextButton : System.Web.UI.Page
        {
                private void Page_Load(object sender, System.EventArgs e)
                {
                        //Get QueryString parms
                        string sText = Request.QueryString["T"]; //Text
                        string sTextColor = Request.QueryString["C"]; //Text Color
                        string sButtonColor = Request.QueryString["BC"]; //Button Color
                        string sFontFace = Request.QueryString["FF"]; //Font Face
                        string sFontSize = Request.QueryString["FS"]; //Font Size
                        string sFontStyle = Request.QueryString["FST"]; //Font Style

                        //If QueryString parms are null set default values
                        if(sText == null) sText = "Schedule";
                        if(sTextColor == null) sTextColor = "Cyan";
                        if(sButtonColor == null) sButtonColor = "Navy";
                        if(sFontFace == null) sFontFace = "Verdana";
                        if(sFontSize == null) sFontSize = "12";
                        if(sFontStyle == null) sFontStyle = "R";

                        //Create Drawing Objects
                        Color TextColor, ButtonColor, CenterColor;
                        try
                        {
                                TextColor = ColorTranslator.FromHtml(sTextColor);
                        }
                        catch(Exception)
                        {
                                TextColor = ColorTranslator.FromHtml("#" + sTextColor);
                        }
                        try
                        {
                                ButtonColor = ColorTranslator.FromHtml(sButtonColor);
                        }
                        catch(Exception)
                        {
                                ButtonColor = ColorTranslator.FromHtml("#" + sButtonColor);
                        }

                        FontStyle TextStyle;
                        switch(sFontStyle.ToUpper())
                        {
                                case "B":
                                        TextStyle = FontStyle.Bold;
                                        break;
                                case "I":
                                        TextStyle = FontStyle.Italic;
                                        break;
                                case "S":
                                        TextStyle = FontStyle.Strikeout;
                                        break;
                                case "U":
                                        TextStyle = FontStyle.Underline;
                                        break;
                                default:
                                        TextStyle = FontStyle.Regular;
                                        break;
                        }

                        Font TextFont = new Font(sFontFace, Int32.Parse(sFontSize),
TextStyle);
                        Bitmap b = new Bitmap(1,1);
                        Graphics g = Graphics.FromImage(b);
                        StringFormat Format = new StringFormat();

                        int Width, Height, Radius;
                        Width = Convert.ToInt32(g.MeasureString(sText, TextFont).Width);
                        Height = Convert.ToInt32(g.MeasureString(sText, TextFont).Height);
                        Radius = Height / 2 + Height / 8;
                        b = new Bitmap(Width + Radius * 2, Height);

                        Color colorFill = Color.FromArgb(255, 255, 255, 255);
                        SolidBrush ButtonBrush = new SolidBrush(ButtonColor);
                        // Render the bitmap
                        g = Graphics.FromImage(b);
                        g.Clear(colorFill);

                        // Draw the top left end cap
                        g.FillPie(ButtonBrush, 0, 0, Radius*2, Height, 180, 90);
                        // Draw the top middle
                        g.FillRectangle(ButtonBrush, Radius, 1, Width, Height/2);
                        // Draw the top right end cap
                        g.FillPie(ButtonBrush, Width, 0, Radius*2, Height, 270, 90);

                        // Draw the bottom left end cap
                        g.FillPie(ButtonBrush, 0, 0, Radius*2, Height, 90, 90);
                        // Draw the bottom middle
                        g.FillRectangle(ButtonBrush, Radius, Height/2+1, Width, Height/2);
                        // Draw the bottom right end cap
                        g.FillPie(ButtonBrush, Width, 0, Radius*2, Height, 360, 90);

                        // Draw Text
                        SolidBrush TextBrush = new SolidBrush(TextColor);
                        g.DrawString(sText, TextFont, TextBrush, new Point(Radius,0),
Format);
                        g.Flush();

                        TextToImage oGIFConvert = new TextToImage();
                        Image NewImg = oGIFConvert.GetGIFWithNewColorTable(b, colorFill);

                        Response.ContentType = "image/gif";
                        NewImg.Save(Response.OutputStream, ImageFormat.Gif);

                        g.Dispose();
                        b.Dispose();
                        NewImg.Dispose();

                }

                private ColorPalette GetColorPalette(uint nColors)
                {
                        // Assume monochrome image.
                        PixelFormat     bitscolordepth = PixelFormat.Format1bppIndexed;
                        ColorPalette    palette;    // The Palette we are stealing
                        Bitmap          bitmap;     // The source of the stolen palette

                        // Determine number of colors.
                        if (nColors > 2)
                                bitscolordepth = PixelFormat.Format4bppIndexed;
                        if (nColors > 16)
                                bitscolordepth = PixelFormat.Format8bppIndexed;

                        // Make a new Bitmap object to get its Palette.
                        bitmap = new Bitmap( 1, 1, bitscolordepth );
                        palette = bitmap.Palette;   // Grab the palette
                        bitmap.Dispose();           // cleanup the source Bitmap
                        return palette;             // Send the palette back
                }

                public Image GetGIFWithNewColorTable(Image image, Color
colTransparent)
                {
                        uint nColors = 256;
                        int Width = image.Width;
                        int Height = image.Height;

                        Bitmap  bitmap = new Bitmap(Width, Height,
PixelFormat.Format8bppIndexed);
                        ColorPalette pal = GetColorPalette(nColors);
                        SortedList oleColours = new SortedList();
                        int[] safe = {0, 51, 102, 153, 204, 255};
                        int cR, cG, cB, cnt = 0;

                        for (cR = 0; cR < safe.Length; cR++)
                        {
                                for (cG = 0; cG < safe.Length; cG++)
                                {
                                        for (cB = 0; cB < safe.Length; cB++)
                                        {
                                                pal.Entries[cnt] = Color.FromArgb(255, safe[cR], safe[cG],
safe[cB]);
                                                oleColours[ColorTranslator.ToOle(pal.Entries[cnt])] = cnt;
                                                cnt++;
                                        }
                                }
                        }
                        pal.Entries[nColors - 1] = Color.FromArgb(0, 255, 255, 255);
                        bitmap.Palette = pal;
                        Bitmap BmpCopy = new Bitmap(Width, Height,
PixelFormat.Format32bppArgb);
                        Graphics g = Graphics.FromImage(BmpCopy);
                        g.PageUnit = GraphicsUnit.Pixel;
                        g.DrawImage(image, 0, 0, Width, Height);
                        g.Dispose();
                        BitmapData  bitmapData;
                        Rectangle rect = new Rectangle(0, 0, Width, Height);
                        bitmapData = bitmap.LockBits(rect, ImageLockMode.WriteOnly,
PixelFormat.Format8bppIndexed);
                        IntPtr pixels = bitmapData.Scan0;

                        unsafe
                        {
                                byte * pBits;
                                if (bitmapData.Stride > 0)
                                        pBits = (byte *)pixels.ToPointer();
                                else
                                        pBits = (byte *)pixels.ToPointer() +
bitmapData.Stride*(Height-1);
                                uint stride = (uint)Math.Abs(bitmapData.Stride);

                                for ( uint row = 0; row < Height; ++row )
                                {
                                        for ( uint col = 0; col < Width; ++col )
                                        {
                                                int colorIndex;
                                                int entry = 0;

                                                Color pixel;    // The source pixel.
                                                byte * p8bppPixel = pBits + row*stride + col;
                                                pixel = BmpCopy.GetPixel((int)col, (int)row);
                                                if (pixel.Equals(colTransparent))
                                                {
                                                        colorIndex = (int)(nColors - 1);
                                                }
                                                else if (oleColours.ContainsKey(ColorTranslator.ToOle(pixel)))
                                                {
                                                        colorIndex = (int)oleColours[ColorTranslator.ToOle(pixel)];
                                                }
                                                else
                                                {
                                                        int thisR, thisG, thisB;

                                                        while(safe[entry] < pixel.R)
                                                        {
                                                                entry += 1;
                                                        }
                                                        thisR = safe[entry];
                                                        entry = 0;

                                                        while(safe[entry] < pixel.G)
                                                        {
                                                                entry += 1;
                                                        }
                                                        thisG = safe[entry];
                                                        entry = 0;

                                                        while(safe[entry] < pixel.B)
                                                        {
                                                                entry += 1;
                                                        }
                                                        thisB = safe[entry];
                                                        entry = 0;

                                                        colorIndex = (int)oleColours[ColorTranslator.ToOle(Color.FromArgb(thisR,
thisG, thisB))];
                                                }
                                                *p8bppPixel = (byte)(colorIndex);
                                        }
                                }
                        }
                        bitmap.UnlockBits(bitmapData);
                        BmpCopy.Dispose();
                        return bitmap;
                }

                #region Web Form Designer generated code
                override protected void OnInit(EventArgs e)
                {
                        //
                        // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                        //
                        InitializeComponent();
                        base.OnInit(e);
                }

                /// <summary>
                /// Required method for Designer support - do not modify
                /// the contents of this method with the code editor.
                /// </summary>
                private void InitializeComponent()
                {    
                        this.Load += new System.EventHandler(this.Page_Load);
                }
                #endregion
        }


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »