Parameter is not valid error in MemoryStream

Encountered the following exception which was caused by converting a byte[] which originated from a BMP to an Image.

 

public static Image ToImage(this byte[] value)

        {

            Image returnImage;

            using (MemoryStream ms = new MemoryStream(value))

            {

                returnImage = Image.FromStream(ms);

            }

 

            return returnImage;

        }

 

System.ArgumentException was unhandled by user code

  HResult=-2147024809

  Message=Parameter is not valid.

  Source=System.Drawing

  StackTrace:

       at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)

       at System.Drawing.Image.FromStream(Stream stream)

 

I will try to solve this in the near future. It works though with all other image formats.

Descriptor Pattern Part II

I described the descriptor patteren earlier, this time I want to elaborate a bit showing the advantage of extending it and using it in combination with an MVC application. I created two classes ApplicationAction and Account Action where AccountAction extends ApplicationAction.

Here is the implementation of the ApplicationAction

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Divelicious.Website.Code.Descriptor
{
public class ApplicationAction
{
private string _description = String.Empty;

private static readonly IDictionary dict = new Dictionary<string, ApplicationAction>();

protected ApplicationAction(string description)
{
_description = description;
dict.Add(description, this);
}

public override string ToString()
{
return _description;
}

public static ApplicationAction Parse(string description)
{
if (dict.Contains(description))
{
return dict[description] as ApplicationAction;
}
else
{
throw new NotImplementedException("This description is currently not supported.");
}
}

public static bool TryParse(string description, out ApplicationAction gender)
{
try
{
gender = Parse(description);
return true;
}
catch (NotImplementedException)
{
gender = null;
return false;
}
}

public static IEnumerable GetMembers()
{
return dict.Values.Cast().ToList();
}
}
}

Here is the AccountAction


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Divelicious.Website.Code.Descriptor
{
public class AccountAction : ApplicationAction
{
private AccountAction(string description)
: base(description)
{

}

// Account
public static readonly AccountAction Login = new AccountAction("Login");
public static readonly AccountAction LogOff = new AccountAction("LogOff");
public static readonly AccountAction Register = new AccountAction("Register");
public static readonly AccountAction Disassociate = new AccountAction("Disassociate");
public static readonly AccountAction Manage = new AccountAction("Manage");
public static readonly AccountAction ExternalLogin = new AccountAction("ExternalLogin");
public static readonly AccountAction ExternalLoginCallback = new AccountAction("ExternalLoginCallback");
public static readonly AccountAction ExternalLoginConfirmation = new AccountAction("ExternalLoginConfirmation");
public static readonly AccountAction ExternalLoginFailure = new AccountAction("ExternalLoginFailure");
public static readonly AccountAction ExternalLoginsList = new AccountAction("ExternalLoginsList");
public static readonly AccountAction RemoveExternalLogins = new AccountAction("RemoveExternalLogins");
}
}

 

And when you want to use it. 

<a href="@Url.Action(@AccountAction.Login.ToString(), "Account", new { Area="" })">Login</a>

The Descriptor Pattern

 This pattern describes the descriptor pattern, this is a usefull pattern to use in MVC development. You could use it to store your actions and controllers in. In the event that the action or controller needs to be changed you can simply change is in the descriptor rather than changing it at all locations in your code.

    public class Genders
    {
        private readonly string _description = String.Empty;
        private static readonly IDictionary dict = new Dictionary<string, Genders>();
        public static readonly Genders Indifferent = new Genders("Indifferent");
        public static readonly Genders Male = new Genders("Male");
        public static readonly Genders Female = new Genders("Female");
        public static readonly Genders Unknown = new Genders("Unknown");

        private Genders(string description)
        {
            _description = description;
            dict.Add(description, this);
        }

        public override string ToString()
        {
            return _description;
        }

        public static Genders Parse(string description)
        {
            if (dict.Contains(description))
            {
                return dict[description] as Genders;
            }
            else
            {
                throw new NotImplementedException("This description is currently not supported.");
            }
        }

        public static bool TryParse(string description, out Genders gender)
        {
            try
            {
                gender = Parse(description);
                return true;
            }
            catch (NotImplementedException)
            {
                gender = null;
                return false;
            }
        }
        
        public static IEnumerable GetMembers()
        {
            return dict.Values.Cast().ToList();
        }
    }

To use it simply use.

Genders.GetMembers

or any other feature you wish to use.

Adding ViewBag value to @Html.Hidden

If you want to assing a viewbag property to an Html Extension try the following.

 

@Html.Hidden("hiddenDate", (object)ViewBag.myDate.ToString("dd.MM.yyyy"))

 

Intellisense for CSS in VS2013 MVC

A very nice feature which is new in Visual Studio 2013 is the code completion / intellisense for MVC pages. It's a bit faded in the example, but you can see the bootstrap pull-left and pull-right.

 

 

Creating an MVC 5 Controller - Empty (VS2013) - There was an error running the selected code generator

Stumbled upon a problem when trying to create a new empty controller. Need to investigate what is going wrong. Workaround is to create a controller by hand. :(

 

 

 

 

 

 

A new beginning

Started this new blog, not sure if the old content will be migrated. Aim is to collect anything related to the .Net framework and share knowledge.

Sponsored by

Calendar

<<  February 2018  >>
MoTuWeThFrSaSu
2930311234
567891011
12131415161718
19202122232425
2627281234
567891011

View posts in large calendar

RecentComments

TextBox