protected override void addImageInfo(int imageNr, Grid cell, string fontFamily, int margin) { if (Vm.IsAddInfo == false) { return; } cell.Margin = new Thickness(margin); MediaFileItem item = Items[imageNr]; TextBlock name = new TextBlock(); name.TextTrimming = TextTrimming.CharacterEllipsis; name.HorizontalAlignment = HorizontalAlignment.Center; name.VerticalAlignment = VerticalAlignment.Bottom; name.Text = Path.GetFileName(item.Location); name.Foreground = new SolidColorBrush(FontColor); Grid.SetRow(name, 0); cell.Children.Add(name); if (item.Metadata == null) { return; } VideoMetadata videoInfo = item.Metadata as VideoMetadata; MediaViewer.MediaDatabase.ImageMetadata imageInfo = item.Metadata as MediaViewer.MediaDatabase.ImageMetadata; TextBlock info = new TextBlock(); info.TextTrimming = TextTrimming.CharacterEllipsis; info.HorizontalAlignment = HorizontalAlignment.Center; info.VerticalAlignment = VerticalAlignment.Top; info.Foreground = new SolidColorBrush(FontColor); String infoText = ""; if (videoInfo != null) { infoText += videoInfo.Width + "x" + videoInfo.Height; } else { infoText += imageInfo.Width + "x" + imageInfo.Height; } infoText += ", " + MiscUtils.formatSizeBytes(item.Metadata.SizeBytes); info.Text = infoText; Grid.SetRow(info, 2); cell.Children.Add(info); }
public override void readMetadata_URLock(MetadataFactory.ReadOptions options, System.Threading.CancellationToken token) { MemoryStream data = new MemoryStream(); String mimeType; try { ItemState = MediaItemState.LOADING; StreamUtils.readHttpRequest(new Uri(ImageInfo.Thumbnail.MediaUrl), data, out mimeType, token); BitmapDecoder decoder = BitmapDecoder.Create(data, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); BitmapSource bitmapSource = decoder.Frames[0]; bitmapSource.Freeze(); ImageMetadata metaData = new ImageMetadata(ImageInfo.MediaUrl, null); metaData.Thumbnail = new Thumbnail(bitmapSource); metaData.Title = ImageInfo.Title; metaData.Location = ImageInfo.MediaUrl; metaData.Width = ImageInfo.Width.HasValue ? ImageInfo.Width.Value : 0; metaData.Height = ImageInfo.Height.HasValue ? ImageInfo.Height.Value : 0; metaData.SizeBytes = ImageInfo.FileSize.HasValue ? ImageInfo.FileSize.Value : 0; metaData.MimeType = ImageInfo.ContentType; Metadata = metaData; ItemState = MediaItemState.LOADED; } catch (Exception e) { if (e is System.Net.WebException && ((System.Net.WebException)e).Status == WebExceptionStatus.Timeout) { ItemState = MediaItemState.TIMED_OUT; } else { ItemState = MediaItemState.ERROR; } } }
public static void writeImage(String outputPath, BitmapSource image, Dictionary<String, Object> options = null, ImageMetadata metaData = null, CancellableOperationProgressBase progress = null) { int width = image.PixelWidth; int height = image.PixelHeight; float scale = ImageUtils.resizeRectangle(width, height, Constants.MAX_THUMBNAIL_WIDTH, Constants.MAX_THUMBNAIL_HEIGHT); TransformedBitmap thumbnail = new TransformedBitmap(image, new System.Windows.Media.ScaleTransform(scale, scale)); if (options != null) { if (options.ContainsKey("Width")) { width = (int)options["Width"]; if (!options.ContainsKey("Height")) { height = (int)(((float)width / image.PixelWidth) * image.PixelHeight); } } if (options.ContainsKey("Height")) { height = (int)options["Height"]; if (!options.ContainsKey("Width")) { width = (int)(((float)height / image.PixelHeight) * image.PixelWidth); } } } BitmapSource outImage = image; if (width != image.PixelWidth || height != image.PixelHeight) { outImage = new TransformedBitmap(image, new System.Windows.Media.ScaleTransform((double)width / image.PixelWidth, (double)height / image.PixelHeight)); } ImageFormat format = MediaFormatConvert.fileNameToImageFormat(outputPath); BitmapEncoder encoder = null; if (format == ImageFormat.Jpeg) { encoder = configureJpeg(options, ref thumbnail); } else if (format == ImageFormat.Png) { encoder = configurePng(options); } else if (format == ImageFormat.Gif) { encoder = new GifBitmapEncoder(); } else if (format == ImageFormat.Bmp) { encoder = new BmpBitmapEncoder(); } else if (format == ImageFormat.Tiff) { encoder = configureTiff(options); } encoder.Frames.Add(BitmapFrame.Create(outImage, thumbnail, null, null)); FileStream outputFile = new FileStream(outputPath, FileMode.Create); encoder.Save(outputFile); outputFile.Close(); if (metaData != null) { metaData.Location = outputPath; ImageFileMetadataWriter metadataWriter = new ImageFileMetadataWriter(); metadataWriter.writeMetadata(metaData, progress); } }
void getImageProperties(ObservableCollection<Tuple<String, String>> p, ImageMetadata image) { p.Add(new Tuple<string, string>("", "IMAGE")); p.Add(new Tuple<string, string>("Image Container", image.ImageContainer)); p.Add(new Tuple<string, string>("Resolution", image.Width + " x " + image.Height)); p.Add(new Tuple<string, string>("Pixel Format", image.PixelFormat)); p.Add(new Tuple<string, string>("Bits Per Pixel", image.BitsPerPixel.ToString())); }
public void generateThumbnail(MediaProbe mediaProbe, ImageMetadata image, CancellationToken token, int timeoutSeconds, int nrThumbnails) { // possibly could not seek in video, try to get the first frame in the video List<MediaThumb> thumbBitmaps = mediaProbe.grabThumbnails(Constants.MAX_THUMBNAIL_WIDTH, Constants.MAX_THUMBNAIL_HEIGHT, 0, 1, 0, token, timeoutSeconds, null); if (thumbBitmaps.Count > 0) { BitmapSource thumb = thumbBitmaps[0].Thumb; if (image.Orientation.HasValue && image.Orientation.Value != 1) { System.Windows.Media.Transform transform = null; switch (image.Orientation.Value) { case 2: { //Mirror horizontal transform = new System.Windows.Media.MatrixTransform(-1, 0, 0, 1, 0, 0); break; } case 3: { //Rotate 180° transform = new System.Windows.Media.RotateTransform(180); break; } case 4: { //Mirror vertical transform = new System.Windows.Media.MatrixTransform(1, 0, 0, -1, 0, 0); break; } case 5: { //Mirror horizontal, rotate 270° transform = new System.Windows.Media.MatrixTransform(0, -1, -1, 0, 0, 0); break; } case 6: { //Rotate 90° transform = new System.Windows.Media.RotateTransform(90); break; } case 7: { //Mirror horizontal, rotate 90° transform = new System.Windows.Media.MatrixTransform(0, 1, 1, 0, 0, 0); break; } case 8: { //Rotate 270° transform = new System.Windows.Media.RotateTransform(270); break; } default: { Logger.Log.Warn("Unknown orientation for image"); break; } } if (transform != null) { thumb = new TransformedBitmap(thumb, transform); } } image.Thumbnail = new Thumbnail(thumb); } else { image.Thumbnail = null; } }
public static BaseMetadata read(String location, MetadataFactory.ReadOptions options, CancellationToken token, int timeoutSeconds) { BaseMetadata metadata = new UnknownMetadata(FileUtils.getPathWithoutFileName(location)); metadata.Name = Path.GetFileName(location); Logger.Log.Info("Reading metadata for: " + location); int timeoutMs = timeoutSeconds * 1000; Stream data = FileUtils.waitForFileAccess(location, FileAccess.Read, timeoutMs, token); MediaProbe mediaProbe = new MediaProbe(); try { mediaProbe.open(location, token); switch (mediaProbe.MediaType) { case MediaType.AUDIO_MEDIA: { metadata = new AudioMetadata(location, data); AudioFileMetadataReader reader = new AudioFileMetadataReader(); reader.readMetadata(mediaProbe, data, options, metadata, token, timeoutSeconds); break; } case MediaType.IMAGE_MEDIA: { metadata = new ImageMetadata(location, data); ImageFileMetadataReader reader = new ImageFileMetadataReader(); reader.readMetadata(mediaProbe, data, options, metadata, token, timeoutSeconds); break; } case MediaType.VIDEO_MEDIA: { metadata = new VideoMetadata(location, data); VideoFileMetadataReader reader = new VideoFileMetadataReader(); reader.readMetadata(mediaProbe, data, options, metadata, token, timeoutSeconds); break; } default: break; } FileInfo info = new FileInfo(location); info.Refresh(); if (info.Attributes.HasFlag(FileAttributes.ReadOnly)) { metadata.IsReadOnly = true; } if (!options.HasFlag(MetadataFactory.ReadOptions.LEAVE_STREAM_OPENED_AFTER_READ)) { metadata.close(); } } catch (Exception e) { metadata.MetadataReadError = e; } finally { mediaProbe.close(); mediaProbe.Dispose(); } return metadata; }