Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

September 2009 - Posts

IIS won’t start – Error 13: The data is invalid.

After restarting my virtual development machine IIS was not able to start anymore. I checked out if there is something reported to Event Viewer and I found the following error message – Error 12: The data is invalid. It turned out that during shutdown of my virtual machine IIS config file was ruined. Solution to my problem was very simple.

IIS configuration is in file c:\windows\system32\inetsrv\applicationHost.config. In my case this file was same by size as before but it was filled with null bytes. When changes are made to this file then previous version is backed up to c:\inetpub\history\ directory.

I copied last working configuration file from history directory to inetsrv directory and started www service. Luckily, everything works again.

If you need more specific description about this error then GrinGod blog has very good posting to read: When IIS Wont Start – Error 13.

Posted: Sep 27 2009, 02:39 AM by DigiMortal | with 3 comment(s)
Filed under:
Creating squared avatar images

Gunnar In one of my current projects I needed something that takes image uploaded by user and converts it to avatar image. There is requirement for avatar images: they must be 32x32 pixel squares. Here is my solution.

Creating squared thumbnail image from what ever user uploaded is pretty simple process. It is illustrated on image you can see on right side of this posting. Here are some explanations.

Creating avatar image: the process

  1. Detect image size
    It doesn’t really matter what dimensions image has. I just wanted to make it visually easier to understand how my algorithm works.
     
  2. Detect shorter size
    To take a square we have to take one of image sides as square side. We take shorter side to avoid some resizing operations that may affect the quality of avatar image we are creating. We will use Math.Mix to detect correct side length of square.
     
  3. Focus square to center
    Usually pictures are taken with focus on some object and this object is located it the center area of picture. Today it is not hard to use photo as avatar, so let’s consider this as a default behavior of end user. To get center area we subtract square side length from original imade size and divide the result by two.
     
  4. Create new image from square
    After steps above we have detected the area of original image we want to use as avatar. Now we will copy this area to new avatar image and save avatar to disc (or database or some other place).

Now you should understand what we are going to do and it’s time to show you my code. I have small and easy method that does all the work mentioned above. I am using classes from System.Drawing and these classes are really powerful. Here is my method.


public void CreateAvatar(int sideLength, Stream fromStream, Stream toStream)

{

    using(var image = System.Drawing.Image.FromStream(fromStream))

    using (var thumbBitmap = new Bitmap(sideLength, sideLength))

    {

        var a = Math.Min(image.Width, image.Height);

        var x = (image.Width - a)/2;

        var y = (image.Height - a)/2;

 

        using (var thumbGraph = Graphics.FromImage(thumbBitmap))

        {

            thumbGraph.CompositingQuality = CompositingQuality.HighQuality;

            thumbGraph.SmoothingMode = SmoothingMode.HighQuality;

            thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

 

            var imgRectangle = new Rectangle(0, 0, sideLength, sideLength);

            thumbGraph.DrawImage(image, imgRectangle, x, y, a, a, GraphicsUnit.Pixel);

            thumbBitmap.Save(toStream, ImageFormat.Png);

        }

    }

}


If you know some better method how to write this code then please let me know! :)

Posted: Sep 16 2009, 02:28 PM by DigiMortal | with 8 comment(s)
Filed under: ,
Using LINQ to query object hierarchies

I used LINQ to solve the following problem: find all titles of objects at hierarchy level X when you know object ID in hierarchy level Y. I cannot imagine if there is some other solution that is same short and clear as one that LINQ provides. Take a look and decide by yourself.

linq-levels-example Here is simple diagram with my entities. Here are my simple rules. Level1 has no parent level and my contain one ore more Level2 entities. Level2 entities have one Level1 parent and one or more Level3 enitities. Level3 entities have one Level2 parent entity and collection of one or more Items. So there is many-to-many relationship between Level3 and Items.

We cannot use composite pattern here because these classes will be very different and there will be no point where we need one common interface for them. That’s why we have one class per level. Also the number of levels is fixed and there is no plan to expand this hierarchy.

By the way, you can model arbitrary class hierarchies on this model and still use this example (as long as it doesn’t hurt performance and you are really sure what you are doing).

Excercise: having Level1 items collection and knowing Level3 item ID find all Items for specified Level3 item and return string of their titles separated by comma. As you don’t have access to source code of data source you must use IList<Level1> and LINQ.

We will use simple class structure given below and we expect that we already got list of Level1 items from some repository or data context.


public class Level1

{

    public int Id { get; set; }

    public string Title { get; set; }

    public IList<Level2> Level2Items { get; set; }

}

 

public class Level2

{

    public int Id { get; set; }

    public string Title { get; set; }

    public Level1 Parent { get; set; }

    public IList<Level3> Level3Items { get; set; }

}

 

public class Level3

{

    public int Id { get; set; }

    public string Title { get; set; }

    public Level2 Parent { get; set; }

    public IList<Item> Items { get; set; }

}

 

public class Item

{

    public int Id { get; set; }

    public string Title { get; set; }

}


Now it’s time to write some hardcore loops and create a cool code-hell… or maybe it’s time to be elegant and use LINQ as stated before. Using LINQ we can provide the following solution:


public string GetItemsStringForLevel3(IList<Level1> level1Items, int level3Id)

{           

    var items = from l in level1Items

                  from l2 in l.Level2Items

                  from l3 in l2.Level3Items

                  from p in l3.Items

                where l3.Id == level3Id

                select p.Title;

 

    return string.Join(", ", items.ToArray());

}


That’s it guys. As you can see you can use LINQ to query your data structures more easily than before. As always – check out if your solutions doesn’t affect security or performance of your system before you are saying that your task is done. :)

Update: Check out how to simulate IN Clause in LINQ queries!


kick it on DotNetKicks.com pimp it 顶 Progg it Shout it
Posted: Sep 15 2009, 09:18 AM by DigiMortal | with 12 comment(s)
Filed under: ,
More Posts