예제 #1
0
 public override MemoryStream ExtractStream(string pathToFile)
 {
     try
     {
         var thumbnailBytes = OleDataReader.GetRawBytes(pathToFile, RevitFileMap.OleStreams.IMAGE_STREAM);
         return(ExtractStream(thumbnailBytes));
     }
     catch (Exception ex)
     {
         throw new InvalidDataException($"Failed to extract the thumbnail of the following Revit file \"{pathToFile}\"", ex);
     }
 }
예제 #2
0
 public override MemoryStream ExtractStream(MemoryStream memoryStream)
 {
     try
     {
         var thumbnailBytes = OleDataReader.GetRawBytes(memoryStream, RevitFileMap.OleStreams.IMAGE_STREAM);
         return(ExtractStream(thumbnailBytes));
     }
     catch (Exception ex)
     {
         throw new InvalidDataException($"Failed to extract the thumbnail of the Revit file", ex);
     }
 }
예제 #3
0
        /// <summary>
        /// Gets the properties.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <returns></returns>
        internal static Dictionary <string, string> GetProperties(string filePath)
        {
            var basicInfo           = OleDataReader.GetRawBytes(filePath, "BasicFileInfo");
            var fileProps           = new Dictionary <string, string>();
            var supportedEncodeings = new[]
            {
                Encoding.Unicode,
                Encoding.BigEndianUnicode,
                Encoding.UTF8,
                Encoding.ASCII,
                Encoding.Default,
            };

            foreach (var encoding in supportedEncodeings)
            {
                var basicInfoString = encoding.GetString(basicInfo)?.TrimEnd('\u0a0d');
                using var basicInfoReader = new StringReader(basicInfoString);

                // ReSharper disable once RedundantAssignment
                var stringLine = basicInfoReader.ReadLine(); // skip the first line
                while (!string.IsNullOrWhiteSpace(stringLine = basicInfoReader.ReadLine()))
                {
                    var parts = stringLine.Split(new[] { ":" }, 2, StringSplitOptions.None);
                    if (parts.Length != 2)
                    {
                        continue;
                    }
                    fileProps.Add(parts[0].Trim(), parts[1].Trim());
                }

                if (fileProps.Any())
                {
                    break;
                }
            }

            return(fileProps);
        }
        /// <summary>
        /// Extracts the stream.
        /// </summary>
        /// <param name="pathToFile">The path to file.</param>
        /// <returns></returns>
        /// <exception cref="InvalidDataException"></exception>
        public override MemoryStream ExtractStream(string pathToFile)
        {
            try
            {
                var thumbnailBytes = OleDataReader.GetRawBytes(pathToFile, RevitFileMap.OleStorage.IMAGE_STREAM);
                // Validate preview data or go out
                if ((thumbnailBytes == null) || (thumbnailBytes.Length <= 0))
                {
                    return(null);
                }

                // read past the Revit meta-data to the start of the PNG image
                var startingOffset = GetPngOffset(thumbnailBytes);
                if (startingOffset == 0)
                {
                    return(null);
                }

                var previewUpperBound = thumbnailBytes.GetUpperBound(0);
                var pngDataBuffer     = new byte[previewUpperBound - startingOffset + 1];

                using (var ms = new MemoryStream(thumbnailBytes))
                {
                    ms.Position = startingOffset;
                    ms.Read(pngDataBuffer, 0, pngDataBuffer.Length);

                    // read the PNG image data into a byte array
                    var outms = new MemoryStream(pngDataBuffer);
                    return(outms);
                }
            }
            catch (Exception ex)
            {
                throw new InvalidDataException($"Failed to extract the thumbnail of the following Revit file \"{pathToFile}\"", ex);
            }
        }