Go Top

2019年2月11日 星期一

[C#] Validate image from file

This article is quoted from https://stackoverflow.com/questions/210650/validate-image-from-file-in-c-sharp/210677

public enum ImageFormat
{
    bmp,
    jpeg,
    gif,
    tiff,
    png,
    unknown
}

public static ImageFormat GetImageFormat(byte[] bytes)
{
    // see http://www.mikekunz.com/image_file_header.html  
    var bmp    = Encoding.ASCII.GetBytes("BM");     // BMP
    var gif    = Encoding.ASCII.GetBytes("GIF");    // GIF
    var png    = new byte[] { 137, 80, 78, 71 };    // PNG
    var tiff   = new byte[] { 73, 73, 42 };         // TIFF
    var tiff2  = new byte[] { 77, 77, 42 };         // TIFF
    var jpeg   = new byte[] { 255, 216, 255, 224 }; // jpeg
    var jpeg2  = new byte[] { 255, 216, 255, 225 }; // jpeg canon

    if (bmp.SequenceEqual(bytes.Take(bmp.Length)))
        return ImageFormat.bmp;

    if (gif.SequenceEqual(bytes.Take(gif.Length)))
        return ImageFormat.gif;

    if (png.SequenceEqual(bytes.Take(png.Length)))
        return ImageFormat.png;

    if (tiff.SequenceEqual(bytes.Take(tiff.Length)))
        return ImageFormat.tiff;

    if (tiff2.SequenceEqual(bytes.Take(tiff2.Length)))
        return ImageFormat.tiff;

    if (jpeg.SequenceEqual(bytes.Take(jpeg.Length)))
        return ImageFormat.jpeg;

    if (jpeg2.SequenceEqual(bytes.Take(jpeg2.Length)))
        return ImageFormat.jpeg;

    return ImageFormat.unknown;
}
閱讀更多 »

[C#] image type byte arrays

Have you ever needed to know the file type of an image?  Did you know that certain image file types when read as a byte array have the same collection of bytes every time?  Instead of checking for an image type by looking for the extension in the file name it’s best to look at the byte array making up the file and compare it to known image type byte arrays.  
I was working on a project where I needed to download images from a url and store them on a file system.  The downloaded images would change daily and could be any number of file types.  In fact, I didn’t even know if the file I was downloading was actually going to be an image.  I only needed images, but it was possible that the web call could send back something else like a web page or different file type all together.  I only wanted to save the file if it was an image type and if the image type was one of the common ones that I expected. (.png, .jpeg, .bmp, etc)  After looking online for a solution to detect an image file type I came across a very informative post on Stack Overflowwhere someone else was trying to do the same thing.  The answer that made the most sense to me was pointed out that a lot of image types have specific sets of bytes at the beginning of the file type to denote which type of file it is.
For example, in C#:
var bmp = Encoding.ASCII.GetBytes("BM"); // BMP 
var gif = Encoding.ASCII.GetBytes("GIF"); // GIF 
var png = new byte[] { 137, 80, 78, 71 }; // PNG 
var tiff = new byte[] { 73, 73, 42 }; // TIFF 
var tiff2 = new byte[] { 77, 77, 42 }; // TIFF 
var jpeg = new byte[] { 255, 216, 255, 224 }; // jpeg 
var jpeg2 = new byte[] { 255, 216, 255, 225 }; // jpeg canon
The post referenced pretty much all of the image file types that I wanted to use.   Anything jpeg, png, gif, or bmp was exactly what I was expecting to see.  This allowed me to pass in a stream from the url and compare it to any of these byte arrays and detect the file type.  This also enabled me to download and save the files locally with the correct extension.  In the post, KevanTTT’s answer was based off another post’s answerbut KevanTTT modified the solution to use a stream rather than only byte arrays.   It isn’t a huge change but I thought I should credit both posts.  After knowing that each image file type’s initial bytes are the same per file type it makes it easy to work with any number of image file types.  
This information is not life changing information but it makes sense as all file types are denoted in some way or another and probably by more than just writing a file extension at the end of a file name.  I’m happy that I came across it and I hope it benefits you in the future!
If you enjoy this topic or enjoy talking about development of any kind you should check out our available positions at Sparkhound.  Sparkhound is full of people with aligned interests and motivation to provide the best possible solution to any scenario.  There are plenty of great minds to lean on and we like to have fun too!  Feel free to contact me at sam.north@sparkhound.com and/or contact Sparkhound for any further discussions, questions, or feedback.  Woot!
Sam
閱讀更多 »

2019年2月10日 星期日

Blogger 要如何設定版面寬度呢?

目前市場主流的螢幕大部份都是寬螢幕了,
17吋的螢幕寬度1280x1024 等
19吋的螢幕寬度1280x1024 或 1366x768 等
20吋的螢幕寬度1440x990 等

螢幕寬度都已經來到1280為基本的配備了,部落格當然也要跟著調整寬度,讓使用者可以更舒適的在沙發上看文章了。

以下來就教學 Blogger 如何更改版面寬度
Step 1
  1. 先選擇左邊選單的「主題」
  2. 點選右邊「自訂」的選項



Step 2
  1.設定「整篇網誌」的寬度1280像素,我的習慣會多個20的寬度,避免文章旁邊的字會因為螢幕造成看不到的情況發生。
  2.設定右側欄的寬度。


或是假設是用code來修改的話, 搜尋<b:template-skin>如下圖, 修改value的pixel值為1260

閱讀更多 »
Copyright © BCL BLOG | Powered by Blogger