public static ImageInfo?FromStream(Stream stream) { ImageInfo?info = null; if (JpgDecoder.Test(stream)) { info = JpgDecoder.Info(stream); } else if (PngDecoder.Test(stream)) { info = PngDecoder.Info(stream); } else if (BmpDecoder.Test(stream)) { info = BmpDecoder.Info(stream); } else if (GifDecoder.Test(stream)) { info = GifDecoder.Info(stream); } else if (PsdDecoder.Test(stream)) { info = PsdDecoder.Info(stream); } else if (TgaDecoder.Test(stream)) { info = TgaDecoder.Info(stream); } return(info); }
public async Task EnhanceAsync(UploadAssetCommand command, HashSet <string>?tags) { if (command.Type == AssetType.Unknown || command.Type == AssetType.Image) { ImageInfo?imageInfo = null; using (var uploadStream = command.File.OpenRead()) { imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(uploadStream); } if (imageInfo != null) { var isSwapped = imageInfo.IsRotatedOrSwapped; if (isSwapped) { var tempFile = new TempAssetFile(command.File); using (var uploadStream = command.File.OpenRead()) { imageInfo = await assetThumbnailGenerator.FixOrientationAsync(uploadStream, tempFile.Stream); } command.File.Dispose(); command.File = tempFile; } if (command.Type == AssetType.Unknown || isSwapped) { command.Type = AssetType.Image; command.Metadata.SetPixelWidth(imageInfo.PixelWidth); command.Metadata.SetPixelHeight(imageInfo.PixelHeight); } } } if (command.Type == AssetType.Image && tags != null) { tags.Add("image"); var wh = command.Metadata.GetPixelWidth() + command.Metadata.GetPixelWidth(); if (wh > 2000) { tags.Add("image/large"); } else if (wh > 1000) { tags.Add("image/medium"); } else { tags.Add("image/small"); } } }
public async Task EnhanceAsync(MetadataRequest request) { var file = request.File; if (request.Type != MediaType.Unknown) { return; } var mimeType = file.MimeType; ImageInfo?imageInfo = null; await using (var uploadStream = file.OpenRead()) { imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(uploadStream, mimeType); } if (imageInfo != null) { var isSwapped = imageInfo.Orientation > ImageOrientation.TopLeft; if (isSwapped) { var tempFile = TempAssetFile.Create(file); await using (var uploadStream = file.OpenRead()) { await using (var tempStream = tempFile.OpenWrite()) { await assetThumbnailGenerator.FixOrientationAsync(uploadStream, mimeType, tempStream); } } await using (var tempStream = tempFile.OpenRead()) { imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(tempStream, mimeType) ?? imageInfo; } await file.DisposeAsync(); request.File = tempFile; } } if (imageInfo != null) { request.Type = MediaType.Image; request.Metadata.SetPixelWidth(imageInfo.PixelWidth); request.Metadata.SetPixelHeight(imageInfo.PixelHeight); } }
public Task <ImageInfo?> GetImageInfoAsync(Stream source) { ImageInfo?result = null; try { var image = Image.Identify(source); if (image != null) { result = new ImageInfo(image.Width, image.Height); } } catch { result = null; } return(Task.FromResult(result)); }
public Task <ImageInfo?> GetImageInfoAsync(Stream source) { Guard.NotNull(source, nameof(source)); ImageInfo?result = null; try { var image = Image.Identify(source); if (image != null) { result = GetImageInfo(image); } } catch { result = null; } return(Task.FromResult(result)); }
/// <summary> /// Adds an image to the image manager. If the image does not support animation this method does nothing. /// This method creates the image list and spawns the animation thread the first time it is called. /// </summary> public static void Animate(Image image, EventHandler onFrameChangedHandler) { if (image == null) { return; } ImageInfo?imageInfo = null; // See comment in the class header about locking the image ref. #pragma warning disable CA2002 lock (image) { #pragma warning restore CA2002 // could we avoid creating an ImageInfo object if FrameCount == 1 ? imageInfo = new ImageInfo(image); } // If the image is already animating, stop animating it StopAnimate(image, onFrameChangedHandler); // Acquire a writer lock to modify the image info list. If the thread has a reader lock we need to upgrade // it to a writer lock; acquiring a reader lock in this case would block the thread on itself. // If the thread already has a writer lock its ref count will be incremented w/o placing the request in the // writer queue. See ReaderWriterLock.AcquireWriterLock method in the MSDN. bool readerLockHeld = s_rwImgListLock.IsReaderLockHeld; LockCookie lockDowngradeCookie = default; t_threadWriterLockWaitCount++; try { if (readerLockHeld) { lockDowngradeCookie = s_rwImgListLock.UpgradeToWriterLock(Timeout.Infinite); } else { s_rwImgListLock.AcquireWriterLock(Timeout.Infinite); } } finally { t_threadWriterLockWaitCount--; Debug.Assert(t_threadWriterLockWaitCount >= 0, "threadWriterLockWaitCount less than zero."); } try { if (imageInfo.Animated) { // Construct the image array // if (s_imageInfoList == null) { s_imageInfoList = new List <ImageInfo>(); } // Add the new image // imageInfo.FrameChangedHandler = onFrameChangedHandler; s_imageInfoList.Add(imageInfo); // Construct a new timer thread if we haven't already // if (s_animationThread == null) { s_animationThread = new Thread(new ThreadStart(AnimateImages50ms)); s_animationThread.Name = typeof(ImageAnimator).Name; s_animationThread.IsBackground = true; s_animationThread.Start(); } } } finally { if (readerLockHeld) { s_rwImgListLock.DowngradeFromWriterLock(ref lockDowngradeCookie); } else { s_rwImgListLock.ReleaseWriterLock(); } } }
public async Task EnhanceAsync(UploadAssetCommand command) { if (command.Type == AssetType.Unknown || command.Type == AssetType.Image) { var mimeType = command.File.MimeType; ImageInfo?imageInfo = null; await using (var uploadStream = command.File.OpenRead()) { imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(uploadStream, mimeType); } if (imageInfo != null) { var isSwapped = imageInfo.Orientation > ImageOrientation.TopLeft; if (command.File != null && isSwapped) { var tempFile = TempAssetFile.Create(command.File); await using (var uploadStream = command.File.OpenRead()) { await using (var tempStream = tempFile.OpenWrite()) { await assetThumbnailGenerator.FixOrientationAsync(uploadStream, mimeType, tempStream); } } await using (var tempStream = tempFile.OpenRead()) { imageInfo = await assetThumbnailGenerator.GetImageInfoAsync(tempStream, mimeType) ?? imageInfo; } await command.File.DisposeAsync(); command.File = tempFile; } if (command.Type == AssetType.Unknown || isSwapped) { command.Type = AssetType.Image; command.Metadata.SetPixelWidth(imageInfo.PixelWidth); command.Metadata.SetPixelHeight(imageInfo.PixelHeight); } } } if (command.Tags == null) { return; } if (command.Type == AssetType.Image) { command.Tags.Add("image"); var wh = command.Metadata.GetPixelWidth() + command.Metadata.GetPixelWidth(); if (wh > 2000) { command.Tags.Add("image/large"); } else if (wh > 1000) { command.Tags.Add("image/medium"); } else { command.Tags.Add("image/small"); } } }