// 静态委托 public void VideoDataCallbackDelegate(int userId, IntPtr buf, int len, AnyChatCoreSDK.BITMAPINFOHEADER bitMap, int userValue) { int stride = bitMap.biWidth * 3; BitmapSource bs = BitmapSource.Create(bitMap.biWidth, bitMap.biHeight, 96, 96, PixelFormats.Bgr24, null, buf, len, stride); // 将图像进行翻转 TransformedBitmap RotateBitmap = new TransformedBitmap(); RotateBitmap.BeginInit(); RotateBitmap.Source = bs; RotateBitmap.Transform = new RotateTransform(180); RotateBitmap.EndInit(); RotateBitmap.Freeze(); // 异步操作 Action action = new Action(delegate() { Dispatcher.BeginInvoke(new Action(delegate() { if (userId == g_selfUserId) { localVideoImage.Source = RotateBitmap; } else if (userId == g_otherUserId) { remoteVideoImage.Source = RotateBitmap; } }), null); }); action.BeginInvoke(null, null); }
public void RotateTiffImage(string filePath, double rotation) { BitmapFrame transformed; using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { var bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.StreamSource = fileStream; bitmap.EndInit(); bitmap.Freeze(); TransformedBitmap transformedBitmap = new TransformedBitmap(bitmap, new RotateTransform(rotation)); transformedBitmap.Freeze(); transformed = BitmapFrame.Create(transformedBitmap); transformed.Freeze(); } // ファイルを書き換えないとファイルサイズが増大するので一度削除 FileInfo fi = new FileInfo(filePath); fi.Delete(); using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Write)) { TiffBitmapEncoder tiffEncoder = new TiffBitmapEncoder(); tiffEncoder.Frames.Add(transformed); tiffEncoder.Save(fileStream); } }
public override Task <BitmapSource> GetThumbnail(Size size, Size fullSize) { var decodeWidth = (int)Math.Round(fullSize.Width * Math.Min(size.Width / 2 / fullSize.Width, size.Height / 2 / fullSize.Height)); var decodeHeight = (int)Math.Round(fullSize.Height / fullSize.Width * decodeWidth); return(new Task <BitmapSource>(() => { try { using (var ms = Meta.GetTiffStream(true)) { var img = new BitmapImage(); img.BeginInit(); img.StreamSource = ms; img.CacheOption = BitmapCacheOption.OnLoad; img.DecodePixelWidth = decodeWidth; img.DecodePixelHeight = decodeHeight; // specific size to avoid .net's double to int conversion img.EndInit(); var scaled = new TransformedBitmap(img, new ScaleTransform(fullSize.Width / img.PixelWidth, fullSize.Height / img.PixelHeight)); scaled.Freeze(); return scaled; } } catch (Exception e) { ProcessHelper.WriteLog(e.ToString()); return null; } })); }
public ImageData CreateImage(BitmapSource frame, ImageMetaData info) { frame = new TransformedBitmap(frame, new ScaleTransform { ScaleY = -1 }); frame.Freeze(); return(new ImageData(frame, info)); }
public BitmapSource Snapshot(int width, int height) { try { // Adjust GL to desired size lock (gl) { UpdateOpenGLControl((int)width, (int)height); // Render var handler = OpenGLDraw; if (handler != null) { handler(this, eventArgsFast); } else { gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT); } // Render. gl.Blit(IntPtr.Zero); switch (RenderContextType) { case RenderContextType.FBO: case RenderContextType.MultiSampleFBO: { var provider = gl.RenderContextProvider as FBORenderContextProvider; var hBitmap = provider.InternalDIBSection.HBitmap; if (hBitmap != IntPtr.Zero) { // JWH - FOR NEW RENDER METHOD: // The FBORenderContextProvider flips the image vertically, so transform it CopyToWriteableBitmap(provider.InternalDIBSection.Bits, hBitmap); } } break; default: return(null); } } BitmapSource toReturn = new TransformedBitmap(m_writeableBitmap, new ScaleTransform(1.0, -1.0)); toReturn.Freeze(); return(toReturn); } finally { // Update original size UpdateOpenGLControl((int)ActualWidth, (int)ActualHeight); } }
private BitmapSource TransformBitmap(BitmapSource source, Transform transform) { var result = new TransformedBitmap(); result.BeginInit(); result.Source = source; result.Transform = transform; result.EndInit(); result.Freeze(); return(result); }
public BitmapSource GetThumbnail(bool autoZoom = false) { GetOrientation(); var count = Run($"-pp \"{_path}\"", ",").Count; if (count == 0) { return(null); } var suc = Run($"-f -ep{count} -l \"{Path.GetTempPath().TrimEnd('\\')}\" \"{_path}\"", ","); if (suc.Count != 0) { return(null); } try { using (var image = new MagickImage(Path.Combine(Path.GetTempPath(), $"{Path.GetFileNameWithoutExtension(_path)}-preview{count}.jpg"))) { File.Delete(image.FileName); if (_orientation == OrientationType.RightTop) { image.Rotate(90); } else if (_orientation == OrientationType.BottomRight) { image.Rotate(180); } else if (_orientation == OrientationType.LeftBotom) { image.Rotate(270); } if (!autoZoom) { return(image.ToBitmapSource()); } var size = GetSize(); var bitmap = new TransformedBitmap(image.ToBitmapSource(), new ScaleTransform(size.Width / image.Width, size.Height / image.Height)); bitmap.Freeze(); return(bitmap); } } catch (Exception) { return(null); } }
/* * Sets the picture to show from a file path */ private void SetImage(string sFilePath, Rotation iRotation) { //Initialize the new image as a bitmap BitmapImage bitmap = new BitmapImage(); //Set the bitmap source bitmap.BeginInit(); bitmap.UriSource = new Uri(sFilePath, UriKind.Relative); bitmap.CacheOption = BitmapCacheOption.OnLoad; //Release the bitmap for it to be accessed bitmap.EndInit(); bitmap.Freeze(); //Initialize a transformed bitmap for rotation TransformedBitmap transBitmap = new TransformedBitmap(); transBitmap.BeginInit(); transBitmap.Source = bitmap.Clone(); //Set the orientation of the transformed bitmap switch (iRotation) { case Rotation.Left: transBitmap.Transform = new RotateTransform(270); break; case Rotation.Right: transBitmap.Transform = new RotateTransform(90); break; case Rotation.Full: transBitmap.Transform = new RotateTransform(180); break; case Rotation.None: transBitmap.Transform = new RotateTransform(0); break; } //Release the transformed bitmap for it to be accessed transBitmap.EndInit(); transBitmap.Freeze(); //Set the grid background color gridPicture.Background = new SolidColorBrush(Colors.Black); //Set the map visibility mapPicture.Visibility = Visibility.Visible; //Set the new image imgPicture.Source = transBitmap.Clone(); }
public static ImageData CreateFlipped(ImageMetaData info, PixelFormat format, BitmapPalette palette, byte[] pixel_data, int stride) { var bitmap = BitmapSource.Create((int)info.Width, (int)info.Height, DefaultDpiX, DefaultDpiY, format, palette, pixel_data, stride); var flipped = new TransformedBitmap(bitmap, new ScaleTransform { ScaleY = -1 }); flipped.Freeze(); return(new ImageData(flipped, info)); }
public static BitmapSource Resize(this BitmapSource bfPhoto, double nWidth, double nHeight, double rotate = 0, int dpiX = 96, int dpiY = 96) { RotateTransform rotateTransform = new RotateTransform(rotate); ScaleTransform scaleTransform = new ScaleTransform(nWidth / 96 * dpiX / bfPhoto.PixelWidth, nHeight / 96 * dpiY / bfPhoto.PixelHeight, 0, 0); TransformGroup transformGroup = new TransformGroup(); transformGroup.Children.Add(rotateTransform); transformGroup.Children.Add(scaleTransform); TransformedBitmap tb = new TransformedBitmap(bfPhoto, transformGroup); tb.Freeze(); return(tb); }
private BitmapSource TranceSelect(BitmapSource _b, Transform _t) { var tBit = new TransformedBitmap(); return(new Func <Transform, TransformedBitmap> (x => { tBit.BeginInit(); tBit.Source = _b; tBit.Transform = x; tBit.EndInit(); tBit.Freeze(); return tBit; })(_t)); }
public BitmapSource ApplyRotateTransform(int angle, BitmapSource image) { var transformedBitmap = new TransformedBitmap(); transformedBitmap.BeginInit(); transformedBitmap.Source = image ?? throw new ArgumentNullException(nameof(image)); var transform = new RotateTransform(angle); transformedBitmap.Transform = transform; transformedBitmap.EndInit(); transformedBitmap.Freeze(); return(transformedBitmap); }
private static TransformedBitmap CreateTransformedBitmap(BitmapSource source, VCProfile profile) { var transformedBitmap = new TransformedBitmap(); transformedBitmap.BeginInit(); transformedBitmap.Source = source; var transformGroup = new TransformGroup(); transformGroup.Children.Add(new ScaleTransform(profile.FlipHorizontal ? -1 : 1, profile.FlipVertical ? -1 : 1)); transformGroup.Children.Add(new RotateTransform(ConvertRotationToDegrees(profile.Rotation))); transformedBitmap.Transform = transformGroup; transformedBitmap.EndInit(); transformedBitmap.Freeze(); return(transformedBitmap); }
public override DocumentPage GetPage(int page_zero_based) { // Hackity hack WPFDoEvents.DoEvents(); if (null != last_document_page) { last_document_page.Dispose(); last_document_page = null; } int page = page_from + page_zero_based; StatusManager.Instance.UpdateStatus("PDFPrinter", String.Format("Printing page {0} of {1}", page_zero_based + 1, this.PageCount), page_zero_based + 1, this.PageCount, true); // Render a page at 300 DPI... using (Image image = Image.FromStream(new MemoryStream(pdf_renderer.GetPageByDPIAsImage(page, 300)))) { PDFOverlayRenderer.RenderAnnotations(image, pdf_document, page, null); PDFOverlayRenderer.RenderHighlights(image, pdf_document, page); PDFOverlayRenderer.RenderInks(image, pdf_document, page); BitmapSource image_page = BitmapImageTools.CreateBitmapSourceFromImage(image); image_page.Freeze(); DrawingVisual dv = new DrawingVisual(); using (DrawingContext dc = dv.RenderOpen()) { // Rotate the image if its orientation does not match the printer if ( page_size.Width < page_size.Height && image_page.Width > image_page.Height || page_size.Width > page_size.Height && image_page.Width < image_page.Height ) { image_page = new TransformedBitmap(image_page, new RotateTransform(90)); image_page.Freeze(); } dc.DrawImage(image_page, new Rect(0, 0, page_size.Width, page_size.Height)); } ++total_pages_printed; last_document_page = new DocumentPage(dv); return(last_document_page); } }
private void UpdateImage(string name) { imageName = name; if (imagePath != null && imagePath.Exists && ImageSize != null) { double scale = ImageSize.Value; BitmapImage image = new BitmapImage(new Uri(imagePath.FullName)); TransformedBitmap scaledImage = new TransformedBitmap(image, new ScaleTransform(scale, scale)); ImageImg.Source = scaledImage; scaledImage.Freeze(); bitMap = scaledImage; ImageSizeLbl.Content = scaledImage.PixelHeight.ToString() + "x" + scaledImage.PixelWidth.ToString(); } }
public static BitmapSource ToBitmapSource(this ImageInfo imageInfo) { // create image from unmanaged memory pointer int bitsPerPixel = imageInfo.BitsPerPixel; // 8 bit or 24 bit var bytesPerPixel = (bitsPerPixel + 7) / 8; int stride = 4 * ((imageInfo.Width * bytesPerPixel + 3) / 4); var size = imageInfo.Width * imageInfo.Height * bitsPerPixel / 8; var pixelFormat = new PixelFormat(); switch (imageInfo.BitsPerPixel) { case 8: pixelFormat = PixelFormats.Gray8; break; case 24: pixelFormat = PixelFormats.Bgr24; break; } var bs = BitmapSource.Create( imageInfo.Width, imageInfo.Height, 96, 96, pixelFormat, BitmapPalettes.Gray256, imageInfo.Buffer, size, stride); // flip image Up-Down var transformedBmp = new TransformedBitmap(); transformedBmp.BeginInit(); transformedBmp.Source = bs; //transformedBmp.Transform = new ScaleTransform(1, 1); transformedBmp.EndInit(); transformedBmp.Freeze(); return(bs); }
private static TransformedBitmap Scale(BitmapSource src, double scale) { // Set up the transformed thumbnail TransformedBitmap thumb = new TransformedBitmap(); thumb.BeginInit(); thumb.Source = src; TransformGroup transform = new TransformGroup(); // Scale double xScale = Math.Min(1.0, Math.Max(1.0 / (double)src.PixelWidth, scale)); double yScale = Math.Min(1.0, Math.Max(1.0 / (double)src.PixelHeight, scale)); transform.Children.Add(new ScaleTransform(xScale, yScale)); thumb.Transform = transform; thumb.EndInit(); thumb.Freeze(); return(thumb); }
public override Task <BitmapSource> GetThumbnail(Size renderSize) { var fullSize = Meta.GetSize(); return(new Task <BitmapSource>(() => { try { using (var buffer = new MemoryStream(Meta.GetThumbnail())) { if (buffer.Length == 0) { return null; } var img = new BitmapImage(); img.BeginInit(); img.StreamSource = buffer; img.CacheOption = BitmapCacheOption.OnLoad; //// specific renderSize to avoid .net's double to int conversion //img.DecodePixelWidth = Math.Max(1, (int) Math.Floor(renderSize.Width)); //img.DecodePixelHeight = Math.Max(1, (int) Math.Floor(renderSize.Height)); img.EndInit(); var scaled = new TransformedBitmap(img, new ScaleTransform(fullSize.Width / img.PixelWidth, fullSize.Height / img.PixelHeight)); Helper.DpiHack(scaled); scaled.Freeze(); return scaled; } } catch (Exception e) { ProcessHelper.WriteLog(e.ToString()); return null; } })); }
} // proc CurrentPanePropertyChanged public ImageSource Render(int pixelWidth, int pixelHeight, double dpiX = 96.0, double dpiY = 96.0) { var ctrlSize = new Size(Math.Min(ActualWidth, 3000), Math.Min(ActualHeight, 3000)); if (ctrlSize.Width < 10.0 || ctrlSize.Height < 10.0) { return(null); } var targetBmp = new RenderTargetBitmap((int)ctrlSize.Width, (int)ctrlSize.Height, dpiX, dpiY, PixelFormats.Pbgra32); targetBmp.Render(this); var aspectX = pixelWidth / ctrlSize.Width; var aspectY = pixelHeight / ctrlSize.Height; var aspect = aspectX < aspectY ? aspectX : aspectY; var transformed = new TransformedBitmap(targetBmp, new ScaleTransform(aspect, aspect)); transformed.Freeze(); return(transformed); } // func Render
public static BitmapSource CreateTransformedBitmap(BitmapSource source, int rotation, bool flip) { if ((rotation == 0) && !flip) { return(source); } TransformedBitmap transformedBitmap = new TransformedBitmap(); transformedBitmap.BeginInit(); transformedBitmap.Source = source; var transformGroup = new TransformGroup(); transformGroup.Children.Add(new ScaleTransform(1, flip ? -1 : 1)); transformGroup.Children.Add(new RotateTransform(rotation)); transformedBitmap.Transform = transformGroup; transformedBitmap.EndInit(); transformedBitmap.Freeze(); return((BitmapSource)transformedBitmap); }
/// <summary> /// 回転した画像を取得する /// </summary> /// <param name="source">基準画像</param> /// <param name="angle">回転角</param> /// <returns>回転した画像</returns> public static BitmapSource Rotation(this BitmapSource source, int angle) { if (source is null) { throw new ArgumentNullException(nameof(source)); } angle %= 360; if (angle == 0) { return(source); } var bitmap = new TransformedBitmap(); bitmap.BeginInit(); bitmap.Source = source; bitmap.Transform = new RotateTransform(angle); bitmap.EndInit(); bitmap.Freeze(); return(bitmap); }
protected override void DrawImage(Size size) { base.DrawImage(size); if (ImageSource != null) { switch (ImageWidget.MirrorType.Value) { case Enums.inkBrushMirrorType.Both: ImageSource = new TransformedBitmap((BitmapSource)ImageSource, new ScaleTransform(-1, -1)); break; case Enums.inkBrushMirrorType.Horizontal: ImageSource = new TransformedBitmap((BitmapSource)ImageSource, new ScaleTransform(-1, 1)); break; case Enums.inkBrushMirrorType.Vertical: ImageSource = new TransformedBitmap((BitmapSource)ImageSource, new ScaleTransform(1, -1)); break; case Enums.inkBrushMirrorType.NoMirror: break; } ImageSource.Freeze(); Mask = new ImageBrush(ImageSource) { Stretch = Stretch.None, TileMode = ToTileMode(TileType), AlignmentX = ToAlignmentX(ImageWidget.TileHAlign.Value), AlignmentY = ToAlignmentY(ImageWidget.TileVAlign.Value), ViewportUnits = BrushMappingMode.Absolute, Viewport = new Rect(0, 0, size.Width, size.Height) }; } }
public override ImageData Read(IBinaryStream file, ImageMetaData info) { var meta = (TxtMetaData)info; var bitmap = new WriteableBitmap(meta.iWidth, meta.iHeight, ImageData.DefaultDpiX, ImageData.DefaultDpiY, PixelFormats.Bgra32, null); var dir = VFS.GetDirectoryName(meta.FileName); foreach (var tile in meta.Tiles) { var filename = VFS.CombinePath(dir, tile.FileName); using (var input = VFS.OpenBinaryStream(filename)) { var tile_info = DetFormat.Value.ReadMetaData(input) as DetBmpMetaData; if (null == tile_info) { throw new InvalidFormatException("Invalid uGOS tile bitmap."); } if (tile.X >= meta.iWidth || tile.Y >= meta.iHeight) { continue; } var reader = new DetBmpFormat.Reader(input, tile_info); reader.Unpack(); int width = Math.Min(tile_info.iWidth, meta.iWidth - tile.X); int height = Math.Min(tile_info.iHeight, meta.iHeight - tile.Y); var src_rect = new Int32Rect(0, 0, width, height); bitmap.WritePixels(src_rect, reader.Data, reader.Stride, tile.X, tile.Y); } } var flipped = new TransformedBitmap(bitmap, new ScaleTransform { ScaleY = -1 }); flipped.Freeze(); return(new ImageData(flipped, meta)); }
/// <summary> /// <para>Decode image from stream (FileStream when loading from file or MemoryStream when loading from archive.</para> /// <para>A <paramref name="decodeSize"/> higher than the actual resolution will be ignored. /// Note that this is the size in pixel instead of the device-independent size used in WPF.</para> /// <para>Returns null if error occured.</para> /// </summary> public static BitmapSource GetImageSource(Stream stream, SizeInt decodeSize = default) { try { stream.Position = 0; var frame = BitmapFrame.Create(stream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None); var pixelSize = new SizeInt(frame.PixelWidth, frame.PixelHeight); ushort orien = 0; if ((frame.Metadata as BitmapMetadata)?.GetQuery("/app1/ifd/{ushort=274}") is ushort u) { orien = u; } frame = null; //calculate decode size if (decodeSize.Width + decodeSize.Height > 0) { //use pixelSize if decodeSize is too big //DecodePixelWidth / Height is set to PixelWidth / Height anyway in reference source if (decodeSize.Width > pixelSize.Width) { decodeSize.Width = pixelSize.Width; } if (decodeSize.Height > pixelSize.Height) { decodeSize.Height = pixelSize.Height; } //flip decodeSize according to orientation if (orien > 4 && orien < 9) { decodeSize = new SizeInt(decodeSize.Height, decodeSize.Width); } } //init bitmapimage stream.Position = 0; var bi = new BitmapImage(); bi.BeginInit(); bi.CacheOption = BitmapCacheOption.OnLoad; if (pixelSize.Width > 0 && pixelSize.Height > 0) { //setting both DecodePixelWidth and Height will break the aspect ratio var imgRatio = (double)pixelSize.Width / pixelSize.Height; if (decodeSize.Width > 0 && decodeSize.Height > 0) { if (imgRatio > (double)decodeSize.Width / decodeSize.Height) { bi.DecodePixelHeight = decodeSize.Height; } else { bi.DecodePixelWidth = decodeSize.Width; } } else if (decodeSize.Width == 0 && decodeSize.Height > 0) { bi.DecodePixelHeight = decodeSize.Height; } else if (decodeSize.Height == 0 && decodeSize.Width > 0) { bi.DecodePixelWidth = decodeSize.Width; } } bi.StreamSource = stream; bi.EndInit(); bi.Freeze(); if (orien < 2) { return(bi); } //apply orientation based on metadata var tb = new TransformedBitmap(); tb.BeginInit(); tb.Source = bi; switch (orien) { case 2: tb.Transform = new ScaleTransform(-1d, 1d); break; case 3: tb.Transform = new RotateTransform(180d); break; case 4: tb.Transform = new ScaleTransform(1d, -1d); break; case 5: { var tg = new TransformGroup(); tg.Children.Add(new RotateTransform(90d)); tg.Children.Add(new ScaleTransform(-1d, 1d)); tb.Transform = tg; break; } case 6: tb.Transform = new RotateTransform(90d); break; case 7: { var tg = new TransformGroup(); tg.Children.Add(new RotateTransform(90d)); tg.Children.Add(new ScaleTransform(1d, -1d)); tb.Transform = tg; break; } case 8: tb.Transform = new RotateTransform(270d); break; } tb.EndInit(); tb.Freeze(); return(tb); } catch { return(null); } }