Archives

Archives / 2004 / July
  • ASCII Art with variable pixel squares...

    After the recent ASCII art blogs by Omer and Raj, I thought I'd have a little play around as well. I prefer gray-scale ASCII art to color, and decided to follow Raj's approach (one difference in gray-scale palette: " `.~:;I=i+oO08#M"), but allow for variable pixel square sizes. Haven't seen Raj's code, but it looks like 1 pixel always maps to one character, although it's quite common in ASCII art for a set of pixels to map to one character. Let's see some code:

    private static string grayscalepalette=" `.~:;I=i+oO08#M";

    public static
     string GetGrayScaleHtml(Bitmap bm,int pixelsquare)
    {
     
    StringBuilder sb = new StringBuilder();
     
    for(int y=0;y<bm.Height-pixelsquare;y+=pixelsquare)
     
    {
       
    for(int x=0;x<bm.Width-pixelsquare;x+=pixelsquare)
       
    {
         
    int luminationtotal=0;
         
    int lumination=0;
         
    for (int i=0;i<pixelsquare;i++)
          
    {
           
    for (int j=0;j<pixelsquare;j++)
           
    {
             
    Color c = bm.GetPixel(x+i,y+j);
             
    lumination = (int)(c.R*0.3 + c.G*0.59+ c.B*0.11);
             
    luminationtotal+=lumination;
           
    }
          
    }
         
    sb.Append(GetGrayScaleChar((int)(luminationtotal/(pixelsquare*pixelsquare))));
        
    }
        
    sb.Append(Environment.NewLine);
     
    }
      
    return sb.ToString();
    }

    private static char GetGrayScaleChar(int lumination)
    {
      
    int index = (int)(((grayscalepalette.Length-1)/255.0) * lumination);
     
    return grayscalepalette[grayscalepalette.Length-1-index];
    }



    As you can see, we now pass a pixelsquare parameter in, and its squared value maps to one ASCII character.

    See the example ASCII art image below, using pixelsquare=3, so 3x3 pixels now map to one character.

                                            `````~:~```              
                                            ``:o8MMMO~`              
                                      ```````+MOI~.:O#.```           
                                      ``````I+I.`````0+```           
                                      ```.~::::~~~~.`=8```           
                                    ```.~~:::::::::~~~I~````         
                                    ``~~~::::::::::::~~~;~``         
                                    `~~~::::::::::::::~~~:.`         
                                 ```~~~::::::;;;;;:::::~~~.````      
                                 ``.~::::::;;;;;;;;::::~~~~.```      
                                 ``~~:::::;;;;;;;;;;::::~~~~```      
                                 `.~:::::;;;;;;;;;;;::::~~~~.``      
                                 `~~:::::;;;;;;;;;;;;:::~~~~~``      
                              ```.~~::::;;;;;;;;;;;;;:::~~~~~.`      
                              ```~~~:::;;;;;;;II;;;;;::~~~~~~.`      
                              ``.~~::::;;;;;IIIIII;;;:::~~~~~.`      
                              ``.~~::::;;;;IIIIIIII;;;::~~~~~~`      
                              ``~~~:::;;;;IIIIIIIII;;;::~~~~~~`      
                            ```.~~:::;;;;;IIIIIIIII;;;:::~~~~~`      
                            ```~:::::;;;;IIIIIIIIII;;;:::~~~~.`      
                            ``~~:::;;;;;;;;;;IIIIII;;;:::~~~~.`      
                            `~~~::;;;;;;;::::::;;II;;::::~~~~.`      
                            .~:::;;;;;::::;;;;::::;;;::::~~~~``      
                            ..~;:;;;;:::;IIIIII;::::;:::~~~~.``      
                         ````~.~;;;;;::;I======I;:~~::::~~~~.``      
                         ```=M=.:I;;;:;Iii=i++i=I;~~::::~~~~```      
                      ``````;+:~:II;;;=I.``..~i+=I:~~:::~~~~```      
                      ``````.~:;;I=;;II``.i#=``I+=;:~:::~~~.         
                       `````~:;IIIiIII``..I0I.``iiI:~:::~~~`         
                       ````.~;I===iII:``.~:::~``.+=:::::~~~````      
                       ````.:;I===+==.``~:;;::.``i=;:::~~~;````      
                      ````..~;I==ii==.`.:;;I;:~``==;:::~~:00```      
                         `..~:;I=i=I=~.~:;III;~.`=I::::~~i#+         
                         ``.~::;;I;I=;~~:;III;~.`iI::::~:8#:         
                         ```.~::::;I=i;::;II;:~.;=;:::::+#0`         
                         ```.:;;;:;Iio+I;;;;;:~~iI;;;::;8#+`         
                         ```~:;I;;;Iio0o=II;;:;+=I;;;::+##;`         
                         ```~;III;;Iio000o+ii++iII;;;::;0#``         
                         ```~;;IIIIIiO800Ooo++i=II;;:::;::``         
                         ``:=;;III=i0###80oo+ii=I;;;:::;::.`         
                         `~==i==i+O8088###8Oo+i=I;;;::;:::.`         
                         `I=ii+oO##0OO000000O+i=I;;;::::~:``         
                      ```:==ii+oO880OOOOOOoooo+=I;;::::::~``         
                      ``.I==ii+oO0000000Ooo+ii+iI;;::::::.``         
                      ``:===ii+oO00888880Oo+i==iI;:::;::.```         
                      ``I==ii++oO088##880Oo+i====;:::~.`````         
                      `~===ii++oO088###880o+i====;::~`````           
                      `:==iii++oO08####880O+ii==I;::.`````           
                      `~=iiii++oO08888800Oo+iiiiI;:~.``              
                      ``Iii++++oOO0000OOOOOo+I+OI;:~.``              
                      ```;ii++oooOOOOO0080Oi:~=0I::~```              
                         `~=+oOO0888####0i;~~~+O;::~```              
                         `~;.:Ii+oOOo+iI;:::~:#+;::~```              
                         `.i..~~~::;;;;;;;:::O0I;::~```              
                         ``ii:~~~::;;;;;;::I00iI;::~```              
                         ``:+oiI:::;;;;;;=O#0+I;;::~```              
                         ` `;+ooOo++++o0###O+=I;;:::.``              
                         `` `~ioOO08#####0ooi=I;;;:.```              
                        ````.`:=+oO000OOOOoi==III:.```````           
                      ````````~Ii+ooOooOOo++ii=;~.````````           
                      `````````=+i==i+++ooo+=;:~..````````           
                    ``````````.;+o====i++i=I;:~~..````````           
                    ``````````~~i++===iiIII;;:~~..```````            
                    `````````~;::+oi===:;I;;;::~..```````            
                 ```````````.:I:~=++i=:::I;;;::~..``````````         
                 ```````````~I:~.:io+::I:;;;;::~..`````.````         
            `   ```````````.;:~~~.I+;:I;::;;;::~~.`````````` ``      
            ``````````````.:;~.~~~~::I;::~;;::::~.```.`````````      
            ```````````.```~~..~~~~::::::~:;::::~......````````      
            ``````````..````...~~~:~~:::::~;:::~~~..~~...````````    
            ``````````..```....~~~~~::::;:~::::::~.~~~...`````````   
            ``````.```..```....~~~~~:::::;::;;;;:~~::~....````````   
            `````...`...```...~~~~~~:::::;;;;;;::~~::~~~..````````   
            `````.......```...~~~~~:::::::;;;;:::::::::~~..```````   
            `````....~~.``....~~~~~:::::::;;;;::::::::::~~..`````` ` 
            `````.....~~``....~~~~~:::::::;;;;;:::;:::::::~..``````` 
            ``````....~~~.....~~~~~:::::::;;;;;:::;:::::::~..``````` 
            ```````...~~::...~~~~~~::::::;;;;;;::;::::;:::~~..`````` 
            ```````...~~::::~~~~~~:::::::;;;;;;::;:::;;;:::~..`````` 
            ````````...~~:::;::~::::::::::;;;;;;;;:::;;;:::~~.`.```` 
               ``````...~~::::;;::::::::;;;;;;;;;;:::;;;:::~~...```` 
              ````````..~~~~:::::;;;;;:;;;;;;;;;;I;::;;;:::~~..````` 
              `` ``````...~~~~~::::;;;;;;;;;;;;;;;;;;;;;;::~..`````` 
                 ```````....~~~~~~:::::::::::::::::::::~~~...`````   
                  ```````.......~~~~~~~~~~~~~~~~~~~~~~~....``````    
                   ````````.........~~~~~~~~~~~~~.......`````````    
                    ````````````...................````````````      
                    ```````````````````````````````````````````      
                      `````````````````````````````````              
                      `````````````````````````````````              
                          ``    ```````````````` ``                  
    

    Original image can be found here.

    Enjoy toying around!

    Edited, since I forgot to add the GetGrayScaleChar method.

  • SQL 2005 XML WebServices? Yuck...

    At this years TechEd 2004 in Amsterdam, I went to a session regarding XML Web Services, which are basically now embedded into SQL 2005, provided it runs on Windows Server 2003 with the HTTP.SYS kernel. All I heard was how great all this stuff is for interoperability, Java clients connecting to your HTTP SOAP endpoint blah, blah etc...

    I have some reservations about this. In fact, I won't be exposing HTTP endpoints from within SQL Server 2005 (when I get to use it in 2008 sometime ;-) ) to the Internet. Why? Simply because of the fact that I wouldn't want to expose a database server to the Internet. Sure, you configure a firewall and allow only the minimum ports and protocols required, but any network engineer would rather see a database server sitting in a DMZ, and rightly so.

    So, how would you use these cool XML web services features when your SQL Server 2005 box is sitting in its own DMZ? Well, simple, as the presenter of the session told me afterwards - you use ISA server to route the HTTP requests through. Great. Sounds like an extra product that needs to be purchased and a lot of hassle to maintain all these mappings. I'll code my own web service, thanks very much.

    Bottom line (all IMHO): use SQL 2005 XML web services if you require basic interoperability between apps on your local network, otherwise if you want to expose this to external, global apps, just don't go there.

  • Skype's SkypeOut (PC -> Phone) Service now in public beta!

    The P2P telephony service Skype has now officially launched a public beta version of its SkypeOut - PC to Phone service.

    PC-to-PC telephony will remain free of charge. The international rates for SkypeOut can be found at https://secure.skype.com/store/help.pricelist.html . To use the SkypeOut feature, make sure you have downloaded the latest Skype version. When you login to the Skype store, you can now buy credit for your SkypeOut calls.

    These Skype guys are doing a marvellous job!

  • Win32 API InternetGetConnectedState

    Ido Samuelson over on the DotNetJunkies blogs posts this useful P/Invoke snippet to check whether a client has an active Internet connection:

    [DllImport("wininet.dll")]
    private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);

    public
    static bool
    IsConnected()
    {
     
    int connectionDescription = 0;
     
    return InternetGetConnectedState(out connectionDescription, 0);
    }

    Wininet.dll comes standard with Internet Explorer 4.0 and higher.

    Also, don't forget to import System.Runtime.InteropServices, of course. ;-)

  • ASP.NET Impersonation on Windows 2000 - update

    A while ago, I posted about some problems I ran into when using ASP.NET code impersonation on a Windows 2000 Server (SP4). We even logged a Microsoft Support call for it.

    Anyway, as with most time consuming issues, it turned out there was a very simple solution. As it states in the Knowlegde Base article, the local ASPNET account should be granted the 'Act as part of the operating system' privilege (this does not seem to be required on Windows Server 2003). The KB article 306158 forgets to add that this will require an IIS reset for it to have effect.

    Logical if you think about it maybe...in hindsight.

  • About the VS.NET Express beta products

    Having watched Scott Guthrie's presentation at TechEd in Amsterdam last week about the VS.NET 2005 roadmap, and after having toyed around with some of the Express products, I do have to make some comments - and where better to vent them than a blog?

    • First of all - it is not clear to a lot of people what the 'Enterprise' differences are between the full-blown VS.NET 2005 and/or the equivalent Express IDE's. I have noticed (as expected) that the Class Designer and Source Control are not supported, but what else? What is the positioning of the Express products in terms of the 2005 versions of VS.NET? I think a feature matrix might help here.
    • SQL 2005 Express would be more useful with some sort of an interface. A very basic, cut down version of the SQL WorkBench would do miracles.
    • ASP.NET - and this is not just the Express products. Whatever happened to good old code-behind? Why isn't it the default anymore? OK, I can change it. But why does even the sample code in demo's show us ASP.NET mark-up and server side code intermingled? I thought the days of classic ASP were behind us...(just...) ;-)
    • Again on ASP.NET, it's nice to have SQL and XML data source ASP.NET controls in the markup, but does it really belong there? Designers who'd be only interested in the mark-up of the page, would rather not see a stored procedure name specified as an attribute of an UI-less ASP.NET control, surely!? Where's our UI vs. code separation?
    • With these Express products, every MS Access guru could use some wizardry and throw together a data driven WinForms or Web App, and say he's got experience with C# WinForms and Web Apps....that can't be good.

    I might come up with more comments as and when I play around a bit more. These are just early observations.

    What are your thoughts?