English 中文(简体)
如何确定某一形象是否部分腐败? C#
原标题:How do determine if an image is partially corrupt? C#

我的印象是 raw。 我利用以下文字来确定图像是否腐败。

public bool IsValidGDIPlusImage(byte[] imageData)
{
    try
    {
        using (var ms = new MemoryStream(imageData))
        {
            using (var bmp = new Bitmap(ms))
            {
            }
        }
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}

如果图象完全腐败,那么上述法典会起到什么作用,但如果我有某种部分腐败的形象? 与下文的联合项目

“entergraph

我如何确定形象部分腐败?

原始形象如下,中心有直线300x300帕克斯图像。

enter image description here Any guidance is highly appreciated. Thanks

问题回答

探测部分损坏的图像非常困难。

简单化的方法是检查起步和末端 marks是否像这样完整:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;
using System.Drawing;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            try {

                var imgBytes = Helper.DownloadImageAsBytes("https://i.stack.imgur.com/KQI1j.jpg");
                //valid GDI+? Example true!
                Console.WriteLine(IsValidGDIPlusImage(imgBytes));
                //Complete? Example false
                ImageFile imgFile = new ImageFile(imgBytes);
                Console.WriteLine(imgFile.Complete);

            }
            catch(Exception ex)
            {
                Console.Write(ex.Message);
            }
        }

        public static bool IsValidGDIPlusImage(byte[] imageData)
        {
            try
            {
                using (var ms = new MemoryStream(imageData))
                {
                    using (var bmp = new Bitmap(ms))
                    {
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}

public class Helper {
    public static byte[] DownloadImageAsBytes(String url) {
        using (var webClient = new WebClient()) { 
            return webClient.DownloadData(url);
        }
    }
}

public class ImageFile {

    private Types  _eFileType   = Types.FileNotFound;
    private bool   _blComplete  = false             ;
    public bool Complete
    {
        get { return _blComplete; }
    }
    private int    _iEndingNull = 0                 ;

    private readonly byte[] _abTagPNG  = { 137, 80, 78, 71, 13, 10, 26, 10  };
    private readonly byte[] _abTagJPG  = { 255, 216, 255                    };
    private readonly byte[] _abTagGIFa = { 71, 73, 70, 56, 55, 97           };
    private readonly byte[] _abTagGIFb = { 71, 73, 70, 56, 57, 97           };
    private readonly byte[] _abEndPNG  = { 73, 69, 78, 68, 174, 66, 96, 130 };
    private readonly byte[] _abEndJPGa = { 255, 217, 255, 255               };
    private readonly byte[] _abEndJPGb = { 255, 217                         };
    private readonly byte[] _abEndGIF  = { 0, 59                            };

    public enum Types { FileNotFound, FileEmpty, FileNull, FileTooLarge, FileUnrecognized, PNG, JPG, GIFa, GIFb }

    public ImageFile(byte[] abtTmp) {

        _eFileType = Types.FileUnrecognized; // default if found

        //byte[] abtTmp = File.ReadAllBytes(_sFilename);
        // check the length of actual data
        int iLength = abtTmp.Length;
        if(abtTmp[abtTmp.Length - 1] == 0) {
            for(int i = (abtTmp.Length - 1); i > -1; i--) {
                if(abtTmp[i] != 0) {
                    iLength = i;
                    break;
                }
            }
        }
        // check that there is actual data
        if(iLength == 0) {
            _eFileType = Types.FileNull;
        } else {
            _iEndingNull = (abtTmp.Length - iLength);
            // resize the data so we can work with it
            Array.Resize<byte>(ref abtTmp, iLength);
            // get the file type
            if(_StartsWith(abtTmp, _abTagPNG)) {
                _eFileType = Types.PNG;
            } else if(_StartsWith(abtTmp, _abTagJPG)) {
                _eFileType = Types.JPG;
            } else if(_StartsWith(abtTmp, _abTagGIFa)) {
                _eFileType = Types.GIFa;
            } else if(_StartsWith(abtTmp, _abTagGIFb)) {
                _eFileType = Types.GIFb;
            }
            // check the file is complete
            switch(_eFileType) {
                case Types.PNG:
                    _blComplete = _EndsWidth(abtTmp, _abEndPNG);
                    break;
                case Types.JPG:
                    _blComplete = (_EndsWidth(abtTmp, _abEndJPGa) || _EndsWidth(abtTmp, _abEndJPGb));
                    break;
                case Types.GIFa:
                case Types.GIFb:
                    _blComplete = _EndsWidth(abtTmp, _abEndGIF);
                    break;
            }
            // get rid of ending null bytes at caller s option
            //if(_blComplete && cullEndingNullBytes) File.WriteAllBytes(_sFilename, abtTmp);
        }

    }


    public Types  FileType        { get { return _eFileType  ; } }
    public bool   IsComplete      { get { return _blComplete ; } }
    public int    EndingNullBytes { get { return _iEndingNull; } }

    private bool _StartsWith(byte[] data, byte[] search) {
        bool blRet = false;
        if(search.Length <= data.Length) {
            blRet = true;
            for(int i = 0; i < search.Length; i++) {
                if(data[i] != search[i]) {
                    blRet = false;
                    break;
                }
            }
        }
        return blRet; // RETURN
    }

    private bool _EndsWidth(byte[] data, byte[] search) {
        bool blRet = false;
        if(search.Length <= data.Length) {
            int iStart = (data.Length - search.Length);
            blRet = true;
            for(int i = 0; i < search.Length; i++) {
                if(data[iStart + i] != search[i]) {
                    blRet = false;
                    break;
                }
            }
        }
        return blRet; // RETURN
    }
}

这至少对一些(更多)图像有用,但无法在开端和端段之间用损坏的数据探测图像。

References:

You can try some things, but with certain file formats (example: BMP, JPEG to some extent) only a human can ultimately decide if the file is OK or corrupted.

There is open software out for this purpose, recommend to take a look at Bad Peggy (Java). If you are willing to use a bigger library, OpenCV could be useful.

我知道我很晚才来到这里,但我只是想去做同样的事,因为联合评价小组的图像被下载到网络上(有时可能包括一个卫星链接,而后者很容易落空)。

https://www.file-recovery.com/jpg-signature-format.htm https://www.file-recovery.com/jpg-signature-format.htm - 初专制图像总是以最后两部 by子FF D9为终结。 因此,对于联合评价小组的图像,你可以简单地读到最后两条 by子,如果它们不与这些价值观相匹配,那么形象就不完整。 当然,你可能有足够的形象,使其几乎完美无缺,而且这一测试在档案中获得了腐败数据,但作为对不完整档案的快速和 d脏测试,它发挥了作用。

我注意到,从两年前,轮功的解决方案只是这样做的(以及测试FF D9 FF FF,我猜测的是,一些JPEG图像有四幅 by末标记)。

希望会有所帮助。





相关问题
Using QCView and iSight to capture image

I have a QCView that loads a Quartz file which gives you iSights feedback (basically like a QTCaptureView) Everything displays fine The button simply takes a snapshot using the following simple ...

Taking picture with QTCaptureView

Is it possible to simply take a picture and save it somewhere using a QTCaptureView and Apple s built-in iSight? I ve seen lots of tutorials on recording video but none on simply taking a picture. Any ...

Transform rectangular image into trapezoid

In .NET how can I transform an image into a trapezoid. The Matrix class supports rotation, shear, etc, but I can t see a trapezoidal transformation. I m using the usual System.Drawing.* API, but I m ...

Rotate image through Y-axis on web page

What are my options for rotating an image on a web page through the Y-axis? I m not sure if I even have the terminology right. Is this called a rotate or a transform. Most of the searches that I ve ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

Convert from 32-BPP to 8-BPP Indexed (C#)

I need to take a full color JPG Image and remap it s colors to a Indexed palette. The palette will consist of specific colors populated from a database. I need to map each color of the image to it s "...

热门标签