Dear reader of Juri's TechBlog,
I moved my blog to a new domain and a new hosting solution as well. I'm now blogging on juristr.com.

What happens if you think in Java and program C#

I'm quite jumping between the two languages. In the morning at the university I'm usually programming Java and then in the afternoon at work I'm programming in C#.net. This can be quite interesting and also be an advantage to have this kind of view to both of the languages (and also the according expertise). My personal opinion is that usually good programmers emerge from the Java world since I find it highly educational.
Anyway, having this kind of view however, you often then come across funny things you coded such as the following :)

I just needed a way for reading in an image as a byte[]. As a Java programmer, hearing byte[], you would immediately think of some stream etc...and so the following came out:

private byte[] ConvertImageToByte(Image toConvert, ImageFormat format)
{
    byte[] result;

    using (MemoryStream ms = new MemoryStream())
    {
        toConvert.Save(ms, format);
        result = ms.ToArray();
    }

    if (result.Length == 0)
        result = null;

    return result;
}
Well encapsulated and it worked perfectly but the solution could have been so simple ;)
byte[] imageData = File.ReadAllBytes(tifFilePath);

Posts you might also be interested in..

Credits: Hoctro | Jack Book

1 Comments:

Manfred said...

I can imagine that switching between two programmin language is interesting and ofter difficult; an adventure :-)

and often in an adventure one can't see the forest for the trees ;-)

But this happens also to me and I currently program "only" in C#; especially when working with files...

Post a Comment