示例#1
0
        /// <summary>
        /// Prop images to lime light network
        /// </summary>
        /// <param name="image">image object to be proped</param>
        /// <param name="sizeTable">Hashtable,the key is the guid of the image instance, and the element
        /// is ImageSize object which contains the size ID and the width& height the image to be resized to </param>
        /// <param name="originalSizeImageInstanceID">The image instance that contains the original image to be used
        /// as a source for resizing operation</param>
        public static Dictionary <Guid, System.Drawing.Image> PropImage(ImageVerifier.MVAProxy.Image image, Hashtable sizeTable, Guid originalSizeImageInstanceID)
        {
            string ImageInstanceFileName = string.Empty;
            Dictionary <Guid, System.Drawing.Image> dictDestImages = new Dictionary <Guid, System.Drawing.Image>();

            SanImageFolders = new string[] { @"\\vidlabfile01\IMAGES", @"\\i\DRMW\IMAGES" };
            string originalSizeImageFolderPath = SanImageFolders[0] + @"\" + originalSizeImageInstanceID.ToString().Substring(originalSizeImageInstanceID.ToString().Length - 2, 2) + @"\";

            string OriginalImageFileName;

            System.Drawing.Image originalImage;
            System.Drawing.Image destImage;
            // System.Drawing.Image cloneImage;
            string       originalImageFileExtension;
            string       destImageFileFullPath;
            MemoryStream destFileStream;

            try
            {
                OriginalImageFileName = Directory.GetFiles(originalSizeImageFolderPath, "*" + originalSizeImageInstanceID.ToString() + "*")[0];
            }
            catch
            {
                throw new IOException("Original file with ID: " + originalSizeImageInstanceID.ToString() + " does not exist.");
            }
            originalImageFileExtension = new FileInfo(OriginalImageFileName).Extension;
            originalImage = System.Drawing.Image.FromFile(OriginalImageFileName);


            try
            {
                #region resize
                foreach (Guid imInstID in sizeTable.Keys)
                {
                    bool foundmatch = false;
                    MVAProxy.ImageInstance currentImInst = null;
                    foreach (MVAProxy.ImageInstance imInst in image.Instances)
                    {
                        if (imInst.Id == imInstID)
                        {
                            currentImInst = imInst;
                            foundmatch    = true;
                            break;
                        }
                    }

                    if (!foundmatch)
                    {
                        throw new ArgumentException("Key: " + imInstID.ToString() + " in size table not in image instances of the image");
                    }
                    destImageFileFullPath = tempImageFolder + @"\" + imInstID.ToString().Substring(imInstID.ToString().Length - 2, 2) + @"\" + imInstID + originalImageFileExtension;

                    if (originalImage.Width == ((ImageSize)(sizeTable[imInstID])).Width && originalImage.Height == ((ImageSize)(sizeTable[imInstID])).Height)
                    {
                        destFileStream = new MemoryStream(File.ReadAllBytes(OriginalImageFileName));
                    }
                    else
                    {
                        if (originalImage.PixelFormat == PixelFormat.Indexed ||
                            originalImage.PixelFormat == PixelFormat.Format8bppIndexed ||
                            originalImage.PixelFormat == PixelFormat.Format4bppIndexed ||
                            originalImage.PixelFormat == PixelFormat.Format1bppIndexed
                            )
                        {
                            destImage = new Bitmap(((ImageSize)(sizeTable[imInstID])).Width, ((ImageSize)(sizeTable[imInstID])).Height, PixelFormat.Format24bppRgb);
                        }
                        else
                        {
                            destImage = new Bitmap(((ImageSize)(sizeTable[imInstID])).Width, ((ImageSize)(sizeTable[imInstID])).Height, originalImage.PixelFormat);
                        }
                        Graphics graphic = Graphics.FromImage(destImage);
                        graphic.CompositingQuality = CompositingQuality.HighQuality;
                        graphic.SmoothingMode      = SmoothingMode.HighQuality;
                        graphic.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                        graphic.DrawImage(originalImage,
                                          new Rectangle(0, 0, ((ImageSize)(sizeTable[imInstID])).Width, ((ImageSize)(sizeTable[imInstID])).Height)
                                          );
                        try
                        {
                            destFileStream = new MemoryStream();

                            if (originalImageFileExtension.ToLower().EndsWith("jpg") || originalImageFileExtension.ToLower().EndsWith("jpeg"))
                            {
                                System.Drawing.Imaging.Encoder qualityEncoder = System.Drawing.Imaging.Encoder.Quality;
                                EncoderParameter ratio = new EncoderParameter(qualityEncoder, jpgCompressionLevel);
                                // Add the quality parameter to the list
                                EncoderParameters codecParams = new EncoderParameters(1);
                                codecParams.Param[0] = ratio;
                                ImageCodecInfo codecInfo = GetEncoderInfo("image/jpeg");
                                destImage.Save(destFileStream, codecInfo, codecParams);
                                destImage.Dispose();
                                dictDestImages.Add(imInstID, System.Drawing.Image.FromStream(destFileStream));
                                destFileStream.Close();
                            }
                            else if (originalImageFileExtension.ToLower().EndsWith("png"))
                            {
                                dictDestImages.Add(imInstID, System.Drawing.Image.FromStream(destFileStream));
                                destImage.Dispose();
                                destFileStream.Close();
                            }
                            else
                            {
                                destImage.Dispose();
                                destFileStream.Close();
                                throw new ArgumentException("The original Image's format: " + originalImageFileExtension + " is not supported.");
                            }
                        }
                        catch (IOException ex)
                        {
                            throw new IOException("IO error while writing image with ID: " + originalSizeImageInstanceID.ToString() + ".", ex);
                        }
                    }
                    // destImage.Dispose();
                    destFileStream.Close();
                }

                #endregion
            }
            catch (Exception)
            {
                if (originalImage != null)
                {
                    originalImage.Dispose();
                }

                throw;
            }
            //  destFileStream.Close();

            originalImage.Dispose();
            return(dictDestImages);
        }
示例#2
0
        /// <summary>
        /// Main entry point for ImageVerifier application
        /// </summary>
        /// <param name="args">Represents path to tab delimited file or a single ImageID GUID</param>
        /// **TBD** Needs to be refactored to several classes. Option to compare images for a single imageId
        ///
        static void Main(string[] args)
        {
            List <string> imageIDs = new List <string>();
            string        line     = null;

            //using (StreamReader file = new StreamReader(@"C:\Users\V-saeld\Desktop\Image\ImageID.txt"))
            using (StreamReader file = new StreamReader(@"..\..\999.txt"))
            {
                while ((line = file.ReadLine()) != null)
                {
                    char[]   delimiters = new char[] { '\t' };
                    string[] parts      = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < parts.Length; i++)
                    {
                        imageIDs.Add(parts[i]);
                    }
                }
                file.Close();
            }
            string results = @"C:\Users\v-saeld\Desktop\results.log";

            using (StreamWriter logWriter = new StreamWriter(results))
            {
                foreach (string imageID in imageIDs)
                {
                    logWriter.WriteLine("ImageID: {0}", imageID);
                    Console.WriteLine("ImageID: {0}", imageID);
                    CatalogServices    cs = Constants.Proxy;
                    ImageSearchRequest imageSearchRequest      = new ImageSearchRequest();
                    List <ImageVerifier.ImInstDisplay> list    = new List <ImageVerifier.ImInstDisplay>();
                    Dictionary <Guid, string>          dictUrl = new Dictionary <Guid, string>();
                    ImageVerifier.MVAProxy.Image       image   = null;
                    ImageVerifier.ImInstDisplay        im      = new ImageVerifier.ImInstDisplay();
                    im.ImageID = new Guid(imageID);
                    image      = cs.GetImage(im.ImageID);
                    int OriginalImageSizeID  = 0;
                    int ThumbnailImageSizeID = 4;
                    List <ImageVerifier.MVAProxy.ImageSize> imageSizes = ImageVerifier.MVAProxy.ImageSize.Get();
                    ImageVerifier.ImInstDisplay             imInstDisplay = new ImageVerifier.ImInstDisplay();
                    int    originalImageFileWidth = 0, originalImageFileHeight = 0, originalImageFileSize = 0;
                    string originalImageFileExtension = string.Empty;
                    bool   originalFileAvailable = false;
                    bool   originalFileinSanAvailable = true;
                    foreach (ImageInstance iminst in image.Instances)
                    {
                        if (iminst.ImageSizeId == OriginalImageSizeID)
                        {
                            im.OriginalFileGuid = iminst.Id;
                            ImageFileHandler.GetImageFileProperty(iminst.Id, out originalImageFileWidth, out originalImageFileHeight, out originalImageFileSize, out originalImageFileExtension);
                            if (originalImageFileWidth == 0 && originalImageFileHeight == 0 && originalImageFileSize == 0)
                            {
                                Console.WriteLine("ImageID: {0} - There is no image in San folder-{1}.", imageID, originalImageFileExtension);
                                logWriter.WriteLine("ImageID: {0} - There is no image in San folder-{1}.", imageID, originalImageFileExtension);
                                originalFileinSanAvailable = false;
                                break;
                            }
                            else
                            {
                                originalFileAvailable = true;
                                continue;
                            }
                        }
                        else if (iminst.ImageSizeId == ThumbnailImageSizeID)
                        {
                            continue;
                        }
                        else
                        {
                            imInstDisplay.LiveURL = iminst.FileUrl;
                            dictUrl.Add(iminst.Id, imInstDisplay.LiveURL.ToString());
                        }
                    }
                    Hashtable propImageList = new Hashtable();
                    foreach (ImageInstance iminst in image.Instances)
                    {
                        if (!originalFileinSanAvailable)
                        {
                            break;
                        }
                        string sourceImageFileExtension = string.Empty;
                        int    sourceImageFileWidth = 0, sourceImageFileHeight = 0, sourceImageFileSize = 0;
                        int    thumbNailImageSizeID = 4;
                        if (!originalFileAvailable)
                        {
                            logWriter.WriteLine("ImageID: {0}-There is no original image.", imageID);
                            Console.WriteLine("ImageID: {0}-There is no original image.", imageID);
                            break;
                        }
                        ImageFileHandler.GetImageFileProperty(iminst.Id, out sourceImageFileWidth, out sourceImageFileHeight, out sourceImageFileSize, out sourceImageFileExtension);
                        if (iminst.ImageSizeId == OriginalImageSizeID || iminst.ImageSizeId == thumbNailImageSizeID)
                        {
                            continue;
                        }
                        ImageSize imSize1 = new ImageSize();
                        imSize1.Id     = iminst.ImageSizeId;
                        imSize1.Width  = sourceImageFileWidth;
                        imSize1.Height = sourceImageFileHeight;
                        propImageList.Add(iminst.Id, imSize1);
                    }
                    if (originalFileAvailable && originalFileinSanAvailable)
                    {
                        Dictionary <Guid, System.Drawing.Image> dictDestImages = ImageFileHandler.PropImage(image, propImageList, im.OriginalFileGuid);
                        Dictionary <Guid, System.Drawing.Image> dictImages     = new Dictionary <Guid, System.Drawing.Image>();
                        foreach (var pair in dictUrl)
                        {
                            System.Drawing.Image ima = ImageUtil.DownloadImage(pair.Value);
                            dictImages.Add(pair.Key, ima);
                        }
                        foreach (KeyValuePair <Guid, System.Drawing.Image> Origkvp in dictImages)
                        {
                            foreach (KeyValuePair <Guid, System.Drawing.Image> kvp in dictDestImages)
                            {
                                if (Origkvp.Key == kvp.Key)
                                {
                                    bool IsImageSame = ImageUtil.DiffImages(kvp.Value, Origkvp.Value);
                                    if (!IsImageSame)
                                    {
                                        logWriter.WriteLine("InstanceID: {0} Different image.", kvp.Key);
                                        Console.WriteLine("InstanceID: {0} Different image.", kvp.Key);
                                    }
                                    else
                                    {
                                        logWriter.WriteLine("InstanceID: {0} Same image.", kvp.Key);
                                        Console.WriteLine("InstanceID: {0} Same image.", kvp.Key);
                                    }
                                }
                            }
                        }
                    }
                }
                Console.ReadLine();
            }
        }