private void UpdateUI(ImageData imageData, ImageInfo imageInfo, ImageType type) { this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { if (imageData != null && imageInfo != null) { System.Windows.Controls.Image imageBox; if (type.Equals(ImageType.COLOR)) { imageBox = imgColorStream; UpdateColorCanvas(); } else { imageBox = imgDepthStream; } // Mirror the stream Image control imageBox.RenderTransformOrigin = new System.Windows.Point(0.5, 0.5); imageBox.RenderTransform = new ScaleTransform(-1, 1); // Display the stream Bitmap bitmap = imageData.ToBitmap(0, imageInfo.width, imageInfo.height); BitmapSource source = Convert(bitmap, GetPixelFormat(type)); bitmap.Dispose(); imageBox.Source = source; source = null; } })); }
private System.Windows.Media.PixelFormat GetPixelFormat(ImageType type) { if (type.Equals(ImageType.COLOR)) { return(ConvertPixelFormat(colorPixelFormat)); } else { return(ConvertPixelFormat(depthPixelFormat)); } }
/// <summary> /// 检查文件是否被盖章(wmf或emf图片标志) /// </summary> /// <param name="doc"></param> /// <returns></returns> public static bool ValidFile(Document doc) { // Convert VML shapes. foreach (Shape vmlShape in doc.GetChildNodes(NodeType.Shape, true)) { ImageType imageType = vmlShape.ImageData.ImageType; if (imageType.Equals(ImageType.Wmf) || imageType.Equals(ImageType.Emf)) { return(false); } } // Convert DrawingML shapes. //foreach (DrawingML dmlShape in doc.GetChildNodes(NodeType.DrawingML, true)) //{ // ImageType imageType = dmlShape.ImageData.ImageType; // if (imageType.Equals(ImageType.Wmf) || imageType.Equals(ImageType.Emf)) // return false; //} return(true); }
private void SaveColorImagesAndDatapointsToDisk(Bitmap bitmap, String directoryName, int imageId, String imgPrefix, ImageType type, Point3DF32[] mappedPixels) { Int32 unixTimestamp; RectI32 bRect; CroppedBitmap cropped; String dirName; JpegBitmapEncoder encoder; FileStream stream; BitmapSource source = Convert(bitmap, GetPixelFormat(type)); dirName = CheckDirectoryName(directoryName); for (int i = 0; i < landmarkBoundingBoxes.Count; i++) { unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; bRect = landmarkBoundingBoxes.ElementAt(i); Int32Rect croppedRect = new Int32Rect(bRect.x, bRect.y, bRect.w, bRect.h); cropped = new CroppedBitmap(source, croppedRect); if (type.Equals(ImageType.COLOR)) { WriteToFile(dirName, imgPrefix + unixTimestamp + "_" + imageId, bitmap, croppedRect, mappedPixels); } using (stream = new FileStream(dirName + imgPrefix + unixTimestamp + "_" + imageId + ".jpeg", FileMode.Create)) { encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(cropped)); encoder.Save(stream); stream.Dispose(); } } encoder = null; stream = null; cropped = null; bitmap.Dispose(); source = null; GC.Collect(); }
/// <summary> /// Resamples one VML or DrawingML image /// </summary> private static bool ResampleCore(ImageData imageData, SizeF shapeSizeInPoints, int ppi, int jpegQuality) { // The are actually several shape types that can have an image (picture, ole object, ole control), let's skip other shapes. if (imageData == null) { return(false); } // An image can be stored in the shape or linked from somewhere else. Let's skip images that do not store bytes in the shape. byte[] originalBytes = imageData.ImageBytes; if (originalBytes == null) { return(false); } // Ignore metafiles, they are vector drawings and we don't want to resample them. ImageType imageType = imageData.ImageType; if (imageType.Equals(ImageType.Wmf) || imageType.Equals(ImageType.Emf)) { return(false); } try { double shapeWidthInches = ConvertUtil.PointToInch(shapeSizeInPoints.Width); double shapeHeightInches = ConvertUtil.PointToInch(shapeSizeInPoints.Height); // Calculate the current PPI of the image. ImageSize imageSize = imageData.ImageSize; double currentPpiX = imageSize.WidthPixels / shapeWidthInches; double currentPpiY = imageSize.HeightPixels / shapeHeightInches; Console.Write("Image PpiX:{0}, PpiY:{1}. ", (int)currentPpiX, (int)currentPpiY); // Let's resample only if the current PPI is higher than the requested PPI (e.g. we have extra data we can get rid of). if ((currentPpiX <= ppi) || (currentPpiY <= ppi)) { Console.WriteLine("Skipping."); return(false); } using (Image srcImage = imageData.ToImage()) { // Create a new image of such size that it will hold only the pixels required by the desired ppi. int dstWidthPixels = (int)(shapeWidthInches * ppi); int dstHeightPixels = (int)(shapeHeightInches * ppi); using (Bitmap dstImage = new Bitmap(dstWidthPixels, dstHeightPixels)) { // Drawing the source image to the new image scales it to the new size. using (Graphics gr = Graphics.FromImage(dstImage)) { gr.InterpolationMode = InterpolationMode.HighQualityBicubic; gr.DrawImage(srcImage, 0, 0, dstWidthPixels, dstHeightPixels); } // Create JPEG encoder parameters with the quality setting. ImageCodecInfo encoderInfo = GetEncoderInfo(ImageFormat.Jpeg); EncoderParameters encoderParams = new EncoderParameters(); encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, jpegQuality); // Save the image as JPEG to a memory stream. MemoryStream dstStream = new MemoryStream(); dstImage.Save(dstStream, encoderInfo, encoderParams); // If the image saved as JPEG is smaller than the original, store it in the shape. Console.WriteLine("Original size {0}, new size {1}.", originalBytes.Length, dstStream.Length); if (dstStream.Length < originalBytes.Length) { dstStream.Position = 0; imageData.SetImage(dstStream); return(true); } } } } catch (Exception e) { // Catch an exception, log an error and continue if cannot process one of the images for whatever reason. Console.WriteLine("Error processing an image, ignoring. " + e.Message); } return(false); }