0

以前の開発者が私の仕事で作成したカスタム フォーラムの作業を引き継いだところ、小さなバグに遭遇しました。フォーラムでアニメーション GIF をアバターとして使用することは許可されていません。ただし、誰かがアニメーション GIF を取得し、その名前を imagename.jpeg に変更してからアップロードした場合、フォーラムでは画像がアニメーションとして表示されます。

拡張子が .gif でない場合にアニメーションが再生されるとは思いもしなかったため、これは非常に奇妙なバグです。

私の質問は次のとおりです。拡張子がそうでない場合でも、画像がアニメーション gif であるかどうかを (.NET 2.0 で) チェックする方法はありますか?

バラ

4

5 に答える 5

2

画像ファイルが ASP.NET 上のアニメーション GIF であるかどうかを検出する最良の方法は、.NET API 自体を使用することです。VB.NET のサンプル コードを次に示します。

Imports System.Drawing
Imports System.Drawing.Imaging

Public Class ImageHelper

    Shared Function IsAnimatedGif(filepath As String) As Boolean
        Return GetFrameCount(filepath) > 1
    End Function

    Shared Function GetFrameCount(filepath As String) As Integer
        Using bitmap As New Bitmap(filepath)
            Dim dimensions As New FrameDimension(bitmap.FrameDimensionsList(0))
            Return bitmap.GetFrameCount(dimensions)
        End Using
    End Function

End Class

C# を使用した同じサンプル:

using System.Drawing;
using System.Drawing.Imaging;

public class ImageHelper
{
    public static bool IsAnimatedGif(string filepath)
    {
        return GetFrameCount(filepath) > 1;
    }

    public static int GetFrameCount(string filepath)
    {
        using (Bitmap bitmap = new Bitmap(filepath))
        {
            FrameDimension dimensions = new FrameDimension(bitmap.FrameDimensionsList[0]);
            return bitmap.GetFrameCount(dimensions);
        }
    }
}
于 2015-08-05T12:58:03.037 に答える
2

ファイルを開いてヘッダーを分析し、GIF であるかどうかを確認できます。http://www.onicos.com/staff/iz/formats/gif.htmlによると、ヘッダーの最初の 3 バイトは「GIF」である必要があります。

于 2010-04-30T16:09:40.387 に答える
1

これが機能するかどうかはわかりませんが、最後の投稿を確認してください:

http://forums.asp.net/p/1188302/2033730.aspx

于 2010-04-30T16:09:25.033 に答える
1

便宜上、動作する C# コードに変換された PHP コードを次に示します。

byte[] byteCode1 = { 0x00, 0x21, 0xF9, 0x04 };
byte[] byteCode2 = { 0x00, 0x2C };
String strTemp;
byte[] byteContents;
bool bIsAnimatedGif = false;
int iCount;
int iPos = 0;
int iPos1;
int iPos2;
Stream st = null;

// st is a stream previously opened on a file
byteContents = new byte[st.Length];
st.Read(byteContents, 0, (int)st.Length);
strTemp = System.Text.Encoding.ASCII.GetString(byteContents);
byteContents = null;
iCount = 0;
while (iCount < 2)
{
    iPos1 = strTemp.IndexOf(System.Text.Encoding.ASCII.GetString(byteCode1), iPos);
    if (iPos1 == -1) break;
    iPos = iPos1 + 1;
    iPos2 = strTemp.IndexOf(System.Text.Encoding.ASCII.GetString(byteCode2), iPos);
    if (iPos2 == -1) break;
    if ((iPos1 + 8) == iPos2)
        iCount++;
    iPos = iPos2 + 1;
}
if (iCount > 1) bIsAnimatedGif = true;
于 2015-03-27T23:45:22.190 に答える
0

これが、あなたが探しているものを正確に実行するPHPのスニペット(PHPフォーラムから抜粋)です。.NETへの変換は非常に簡単です。

function is_animated_gif($filename)
{
    $filecontents=file_get_contents($filename);

    $str_loc=0;
    $count=0;
    while ($count < 2) 
    {
            $where1=strpos($filecontents,"\x00\x21\xF9\x04",$str_loc);
            if ($where1 === FALSE)
            {
                    break;
            }
            else
            {
                    $str_loc=$where1+1;
                    $where2=strpos($filecontents,"\x00\x2C",$str_loc);
                    if ($where2 === FALSE)
                    {
                            break;
                    }
                    else
                    {
                            if ($where1+8 == $where2)
                            {
                                    $count++;
                            }
                            $str_loc=$where2+1;
                    }
            }
    }

    if ($count > 1)
    {
            return(true);
    }
    else
    {
            return(false);
    }
}
于 2010-04-30T16:12:03.190 に答える