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>

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.

 

 

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

<<  May 2018  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

RecentComments

TextBox