/// <summary> /// Converts the input BitmapSource to the Pbgra32 format WriteableBitmap which is internally used by the WriteableBitmapEx. /// </summary> /// <param name="source">The source bitmap.</param> /// <returns></returns> public static WriteableBitmap ConvertToPbgra32Format(BitmapSource source) { // Convert to Pbgra32 if it's a different format if (source.Format == PixelFormats.Pbgra32) { return new WriteableBitmap(source); } var formatedBitmapSource = new FormatConvertedBitmap(); formatedBitmapSource.BeginInit(); formatedBitmapSource.Source = source; formatedBitmapSource.DestinationFormat = PixelFormats.Pbgra32; formatedBitmapSource.EndInit(); return new WriteableBitmap(formatedBitmapSource); }
private void showImage(string file) { try { BitmapImage bmap = new BitmapImage(); bmap.BeginInit(); bmap.UriSource = new Uri(file, UriKind.Relative); bmap.CacheOption = BitmapCacheOption.OnLoad; bmap.EndInit(); currentFile = new FileInfo(file); lastPoint = new Point(); lastPosition = new Thickness(); currentSize = new Size(bmap.PixelWidth, bmap.PixelHeight); currentStretch = Stretch.Uniform; currentZoom = 1000; if (isGrayScale) { BitmapImage aux = bmap; FormatConvertedBitmap gbmap = new FormatConvertedBitmap(); gbmap.BeginInit(); gbmap.Source = bmap; gbmap.DestinationFormat = PixelFormats.Gray32Float; gbmap.EndInit(); xImage.Source = gbmap; } else { xImage.Source = bmap; } xImage.Stretch = Stretch.Uniform; rePosition(true, false, 0); this.Title = "Ogirdor MasterView | " + currentFile.Name; xZoomValue.Text = ""; xColorValue.Text = ""; } catch (Exception) { } }
public static Image ConvertToImage(BitmapSource bitmapsource) { // convert image format var src = new FormatConvertedBitmap(); src.BeginInit(); src.Source = bitmapsource; src.DestinationFormat = PixelFormats.Bgra32; src.EndInit(); // copy to bitmap var bitmap = new Bitmap(src.PixelWidth, src.PixelHeight, PixelFormat.Format32bppArgb); var data = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); src.CopyPixels(System.Windows.Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); bitmap.UnlockBits(data); return(bitmap); }
public static Bitmap BitmapFromSource(BitmapSource bitmapsource) { //convert image pixel format: var bs32 = new FormatConvertedBitmap(); //inherits from BitmapSource bs32.BeginInit(); bs32.Source = bitmapsource; bs32.DestinationFormat = System.Windows.Media.PixelFormats.Bgra32; bs32.EndInit(); //source = bs32; //now convert it to Bitmap: Bitmap bmp = new Bitmap(bs32.PixelWidth, bs32.PixelHeight, PixelFormat.Format32bppArgb); BitmapData data = bmp.LockBits(new Rectangle(Point.Empty, bmp.Size), ImageLockMode.WriteOnly, bmp.PixelFormat); bs32.CopyPixels(System.Windows.Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); bmp.UnlockBits(data); return(bmp); }
private static byte[] GetPixels(RenderTargetBitmap rtb, int width, int height) { int strideColorBitmap = width * 4 /* 32 BitsPerPixel */; // Convert the bitmap from Pbgra32 to Bgra32 FormatConvertedBitmap converter = new FormatConvertedBitmap(); converter.BeginInit(); converter.Source = rtb; converter.DestinationFormat = PixelFormats.Bgra32; converter.EndInit(); byte[] pixels = new byte[strideColorBitmap * height]; // Call the internal method which skips the MediaPermission Demand converter.CriticalCopyPixels(Int32Rect.Empty, pixels, strideColorBitmap, 0); return(pixels); }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var obj = (value as CategoryTextImage); if (obj.IsCompleteDown) { return(obj.ImageUrl); } else { BitmapImage bitmapImage = new BitmapImage(new Uri(obj.ImageUrl)); FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = bitmapImage; newFormatedBitmapSource.DestinationFormat = PixelFormats.Gray8; newFormatedBitmapSource.EndInit(); return(newFormatedBitmapSource); } }
/// <summary> /// 图片变灰 /// </summary> /// <param name="source"></param> /// <returns></returns> public static BitmapSource ToGray(BitmapSource source) { FormatConvertedBitmap re = new FormatConvertedBitmap(); try { re.BeginInit(); re.Source = source; re.DestinationFormat = PixelFormats.Gray8; re.EndInit(); } catch (Exception ex) { LogManage.WriteLog(typeof(ImageManage), ex); } return(re); }
private static bool TryFromSingleImage(DataObject data, out Surface result) { try { BitmapSource source; if (data.GetDataPresent("PNG")) { source = FromPNG(data); } else if (data.GetDataPresent(DataFormats.Dib) || data.GetDataPresent(DataFormats.Bitmap)) { source = Clipboard.GetImage(); } else { result = null; return(false); } if (source.Format.IsSkiaSupported()) { result = new Surface(source); } else { FormatConvertedBitmap newFormat = new FormatConvertedBitmap(); newFormat.BeginInit(); newFormat.Source = source; newFormat.DestinationFormat = PixelFormats.Bgra32; newFormat.EndInit(); result = new Surface(newFormat); } return(true); } catch { } result = null; return(false); }
private static Watermark CheckGreyMark(Watermark mark, bool blackAndWhite) { if (mark == null || mark.ImageSource == null) { return(null); } if (!blackAndWhite) { return(mark); } var newMark = new Watermark { Opacity = mark.Opacity, Size = mark.Size }; byte[] markBytes; using (var stream = new MemoryStream()) { var encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create((BitmapSource)mark.ImageSource)); encoder.Save(stream); markBytes = stream.ToArray(); } using (var stream = new MemoryStream(markBytes)) { var bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.StreamSource = stream; bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.EndInit(); bitmap.Freeze(); var grayBitmapSource = new FormatConvertedBitmap(); grayBitmapSource.BeginInit(); grayBitmapSource.Source = bitmap; grayBitmapSource.DestinationFormat = PixelFormats.Gray32Float; grayBitmapSource.EndInit(); newMark.ImageSource = grayBitmapSource; } return(newMark); }
private void GrayScale() { /*BitmapImage bitmap = new BitmapImage(); * bitmap.BeginInit(); * bitmap.StreamSource = Image; * bitmap.EndInit();*/ FormatConvertedBitmap grayBitmapSource = new FormatConvertedBitmap(); grayBitmapSource.BeginInit(); //grayBitmapSource.Source = BitmapImageSource; grayBitmapSource.DestinationFormat = PixelFormats.Gray32Float; grayBitmapSource.EndInit(); /*Image grayImage = new Image(); * grayImage.Width = 300; * grayImage.Source = grayBitmapSource;*/ //imageTest.Source = grayBitmapSource; }
private void GrayScaleButton_OnClick(object sender, RoutedEventArgs e) { BitmapImage bitmap = new BitmapImage(); string filePath = FilePath.Text; bitmap.BeginInit(); bitmap.UriSource = new Uri(filePath); bitmap.EndInit(); FormatConvertedBitmap grayBitmapSource = new FormatConvertedBitmap(); grayBitmapSource.BeginInit(); grayBitmapSource.Source = bitmap; grayBitmapSource.DestinationFormat = PixelFormats.Gray32Float; grayBitmapSource.EndInit(); ModifiedImageViewer.Source = grayBitmapSource; }
public static Texture LoadTexture(string fileName) { BitmapSource bitmapSource = new BitmapImage(new Uri(fileName, UriKind.RelativeOrAbsolute)); FormatConvertedBitmap newBitmapSource = new FormatConvertedBitmap(); newBitmapSource.BeginInit(); newBitmapSource.Source = bitmapSource; newBitmapSource.DestinationFormat = PixelFormats.Bgra32; newBitmapSource.EndInit(); byte[] rawBytes = new byte[newBitmapSource.PixelWidth * newBitmapSource.PixelHeight * 4]; newBitmapSource.CopyPixels(rawBytes, newBitmapSource.PixelWidth * 4, 0); Softy.Color[] rawImage = new Softy.Color[newBitmapSource.PixelWidth * newBitmapSource.PixelHeight]; for (int i = 0; i < newBitmapSource.PixelWidth * newBitmapSource.PixelHeight; ++i) { rawImage[i] = new Softy.Color(rawBytes[i * 4 + 2], rawBytes[i * 4 + 1], rawBytes[i * 4 + 0], rawBytes[i * 4 + 3]); } Texture texture = new Texture(rawImage, newBitmapSource.PixelWidth); return(texture); }
public void ExecuteSaveAsCommand(object parameter) { SaveFileDialog saveDlg = new SaveFileDialog(); saveDlg.Filter = "Tiff(Gray8)|*.tif"; if (saveDlg.ShowDialog() == true) { FormatConvertedBitmap convertImage = new FormatConvertedBitmap(); convertImage.BeginInit(); convertImage.Source = Workspace.This.ImageGalleryVM.DisplayImage; convertImage.DestinationFormat = PixelFormats.Gray8; convertImage.EndInit(); TiffBitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(convertImage)); FileStream fileStream = new FileStream(saveDlg.FileName, FileMode.Create, FileAccess.ReadWrite); encoder.Save(fileStream); fileStream.Close(); } }
/// <summary> /// Changes the pixel format. /// </summary> /// <param name="bmpSource">The BitmapSource source.</param> /// <param name="pixFormat">The pix format.</param> /// <returns></returns> public static FormatConvertedBitmap ChangePixelFormat(BitmapSource bmpSource, PixelFormat pixFormat) { if (bmpSource == null) { return(null); } var bmpConverted = new FormatConvertedBitmap(); // Change properties within a BeginInit/EndInit block. bmpConverted.BeginInit(); bmpConverted.Source = bmpSource; // Set to new pixel format. bmpConverted.DestinationFormat = pixFormat; bmpConverted.EndInit(); return(bmpConverted); }
public void Resize(Bitmap src, Bitmap dst, object options = null) { var source = Misc.AllocWriteableBitmap(src.Width, src.Height, src.Depth, src.Channel); Misc.CopyToWritableBitmap(source, src); var scaleTransform = new ScaleTransform((double)dst.Width / src.Width, (double)dst.Height / src.Height); BitmapSource transformed = new TransformedBitmap(source, scaleTransform); if (transformed.Format.BitsPerPixel > 64) { var converted = new FormatConvertedBitmap(); converted.BeginInit(); converted.Source = transformed; converted.DestinationFormat = dst.Channel == 4 ? PixelFormats.Rgba64 : PixelFormats.Rgb48; converted.EndInit(); transformed = converted; } transformed.CopyPixels(Int32Rect.Empty, dst.Scan0, dst.Stride * dst.Height, dst.Stride); }
private void TestingButton() { // If no image is loaded --- AVOID NULL REFERENCE if (ImageViewWindow.Source == null) { return; } // make it gray FormatConvertedBitmap grayBitmap = new FormatConvertedBitmap(); grayBitmap.BeginInit(); grayBitmap.Source = bitmap; grayBitmap.DestinationFormat = PixelFormats.Gray8; grayBitmap.EndInit(); ImageViewWindow.Source = grayBitmap; }
//asp.net 把图片RGB格式转换成CMYK印刷格式 //http://blog.sina.com.cn/s/blog_78106bb10101d61t.html public static void convert2Cmyk() { Stream imageStream = new FileStream(@"C:\Users\min\Desktop\RGB.jpg", FileMode.Open, FileAccess.Read, FileShare.Read); BitmapSource myBitmapSource = BitmapFrame.Create(imageStream); FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = myBitmapSource; newFormatedBitmapSource.DestinationFormat = PixelFormats.Cmyk32; newFormatedBitmapSource.EndInit(); BitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(newFormatedBitmapSource)); Stream cmykStream = new FileStream(@"C:\Users\min\Desktop\Cmyk32.jpg", FileMode.Create, FileAccess.Write, FileShare.Write); encoder.Save(cmykStream); cmykStream.Close(); }
public HostTile(HostInstall model) { InitializeComponent(); _model = model; this.DataContext = model; try { BitmapImage source = new BitmapImage(); source.BeginInit(); source.UriSource = new Uri($"pack://application:,,,/Assets/{model.Name}.jpg", UriKind.Absolute); source.EndInit(); if (!model.HostInstalled) { FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = source; newFormatedBitmapSource.DestinationFormat = PixelFormats.Gray32Float; newFormatedBitmapSource.EndInit(); Banner.Source = newFormatedBitmapSource; } else { Banner.Source = source; } } catch (Exception e) { Console.WriteLine(e); } this._model.PropertyChanged += (sender, args) => { if (args.PropertyName == nameof(HostInstall.Active) && _model.Active != null) { Xceed.Wpf.Toolkit.MessageBox.Show("Application installed", "", MessageBoxButton.OK, MessageBoxImage.Information); } }; }
public SixelRawImage(BitmapSource bitmap) { // https://docs.microsoft.com/en-us/dotnet/framework/wpf/graphics-multimedia/how-to-convert-a-bitmapsource-to-a-different-pixelformat FormatConvertedBitmap formatConvertedBitmap = new FormatConvertedBitmap(); formatConvertedBitmap.BeginInit(); formatConvertedBitmap.Source = bitmap; formatConvertedBitmap.DestinationFormat = PixelFormats.Bgr24; formatConvertedBitmap.EndInit(); var(width, height) = (formatConvertedBitmap.PixelWidth, formatConvertedBitmap.PixelHeight); var stride = (width * formatConvertedBitmap.Format.BitsPerPixel + 7) / 8; // 8バイトづつ var rawpixeldata = new byte[stride * height]; formatConvertedBitmap.CopyPixels(rawpixeldata, stride, 0); StringBuilder builder = new StringBuilder(); for (int i = 0; i < (height + 5) / 6; i++) { for (int k = 0; k < width; k++) { int item = 63; for (int j = 0; j < 6; j++) { int index = (6 * i + j) * width * 3 + k * 3; if (index < stride * height && rawpixeldata[index] == 0xFF) { item += 1 << j; } } builder.Append(char.ConvertFromUtf32(item)); } builder.Append("-"); // create new line } instr = builder.ToString(); }
public void Open(string path) { filePath = path; // 读取图片 BitmapImage sourceImage = new BitmapImage(); sourceImage.BeginInit(); sourceImage.UriSource = new Uri(AppDomain.CurrentDomain.BaseDirectory + path, UriKind.Absolute);//打开图片 sourceImage.EndInit(); BitmapSource pixelImage = sourceImage; // 设置图片格式 rgba FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = pixelImage; newFormatedBitmapSource.DestinationFormat = PixelFormats.Bgra32; newFormatedBitmapSource.EndInit(); pixelImage = newFormatedBitmapSource; // 获得图片数据 width = pixelImage.PixelWidth; height = pixelImage.PixelHeight; int stride = pixelImage.PixelWidth * 4; byte[] pixels = new byte[pixelImage.PixelHeight * stride]; pixelImage.CopyPixels(pixels, stride, 0); colorData = new Color[width, height]; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { colorData[i, j].b = pixels[(j * width + i) * 4]; colorData[i, j].g = pixels[(j * width + i) * 4 + 1]; colorData[i, j].r = pixels[(j * width + i) * 4 + 2]; colorData[i, j].a = pixels[(j * width + i) * 4 + 3]; } } }
public static FormatConvertedBitmap ConvertToBgr32Format(BitmapSource source) { // Convert to Pbgra32 if it's a different format //if (source.Format == PixelFormats.Bgr32) { // return new WriteableBitmap(source); //} var formattedBitmapSource = new FormatConvertedBitmap(); formattedBitmapSource.BeginInit(); formattedBitmapSource.Source = source; formattedBitmapSource.DestinationFormat = PixelFormats.Bgr32; formattedBitmapSource.EndInit(); //formattedBitmapSource.CopyPixels(); return(formattedBitmapSource); //WriteableBitmap wb = new WriteableBitmap(formatedBitmapSource); ////delete //formatedBitmapSource = null; //GC.Collect(); //return wb; }
private void Map_MenuItem_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Image Files (*.bmp, *.jpg, *.png, *.gif)|*.bmp;*.jpg; *.png; *.gif"; openFileDialog.FilterIndex = 1; if (openFileDialog.ShowDialog() == true) { System.IO.FileInfo File = new System.IO.FileInfo(openFileDialog.FileName); BitmapImage coloredImage = new BitmapImage(new Uri(openFileDialog.FileName)); FormatConvertedBitmap grayBitmap = new FormatConvertedBitmap(); grayBitmap.BeginInit(); grayBitmap.Source = coloredImage; grayBitmap.DestinationFormat = PixelFormats.Gray8; grayBitmap.EndInit(); Map.Background = new ImageBrush(grayBitmap); } }
public static void SetArmySelected(string armySource) { foreach (KeyValuePair <Image, string> image in allImages) { if (String.IsNullOrEmpty(armySource) || (armySource == image.Value)) { image.Key.Source = new BitmapImage(new Uri(image.Value)); } else { FormatConvertedBitmap bwImage = new FormatConvertedBitmap(); bwImage.BeginInit(); bwImage.Source = new BitmapImage(new Uri(image.Value)); bwImage.DestinationFormat = PixelFormats.Gray8; bwImage.EndInit(); image.Key.Source = bwImage; } } }
/// <summary> /// Constructor. /// </summary> /// <param name="bitmap">Source image</param> /// <param name="converter">Converter to use</param> public unsafe ColorImage(BitmapSource bitmap, ColorSpaceConverter converter) { float o1, o2, o3; int rows = bitmap.PixelHeight; int columns = bitmap.PixelWidth; _handler = new FloatArrayHandler(rows, columns, 3); FormatConvertedBitmap bmp = new FormatConvertedBitmap(); bmp.BeginInit(); bmp.Source = bitmap; bmp.DestinationFormat = PixelFormats.Rgb24; bmp.EndInit(); byte[] pixels = new byte[rows * columns * 3]; bmp.CopyPixels(pixels, columns * 3, 0); fixed(byte *src = pixels) { fixed(float *dst = _handler.RawArray) { byte * srcPtr = src; float *dstPtr = dst; int length = rows * columns; while (length-- > 0) { float i1 = *srcPtr++; float i2 = *srcPtr++; float i3 = *srcPtr++; o1 = o2 = o3 = 0; converter(i1, i2, i3, ref o1, ref o2, ref o3); *dstPtr++ = o1; *dstPtr++ = o2; *dstPtr++ = o3; } } } pixels = null; }
/// <summary> /// Construction d'un module à partir de son chemin d'accès /// </summary> /// <param name="path"></param> public Module(string path) { if (File.Exists(path)) { // Enregistrement du nom de fichier du module (avec l'extension) this.file_name = Path.GetFileName(path); // Lecture du fichier app.json du module Stream propertiesFile = Assembly.LoadFile(path).GetManifestResourceStream("Project_Manager_Module.Module.app.json"); StreamReader reader = new StreamReader(propertiesFile); JsonTextReader jsonReader = new JsonTextReader(reader); JObject jsonContent = (JObject)JToken.ReadFrom(jsonReader); // Enregistrement des propriétés this.name = jsonContent["name"].ToString(); this.description = jsonContent["description"].ToString(); this.require_login = (bool)jsonContent["require_login"]; // Fermeture du lecteur jsonReader.Close(); reader.Close(); // Lecture du logo du module Stream imgStream = Assembly.LoadFile(path).GetManifestResourceStream("Project_Manager_Module.Module.logo.png"); BitmapImage logo = new BitmapImage(); logo.BeginInit(); logo.StreamSource = imgStream; logo.EndInit(); imgStream.Close(); this.logo = logo; // Création de la version en nuances de gris du logo FormatConvertedBitmap logoGreyScaled = new FormatConvertedBitmap(); logoGreyScaled.BeginInit(); logoGreyScaled.Source = this.logo; logoGreyScaled.DestinationFormat = PixelFormats.Gray32Float; logoGreyScaled.EndInit(); this.logo_greyscale = logoGreyScaled; } }
private void getBackImageSource(BitmapSource i) { if (i == null) { EnablebackImage = null; unEnablebackImage = null; return; } FormatConvertedBitmap b = new FormatConvertedBitmap(); b.BeginInit(); b.Source = i; b.DestinationFormat = PixelFormats.Gray8; b.EndInit(); FormatConvertedBitmap b1 = new FormatConvertedBitmap(); b1.BeginInit(); b1.Source = i; b1.EndInit(); EnablebackImage = b1; unEnablebackImage = b; }
private static Image CreateIcon(drawing.Bitmap image, out Image grayIcon) { var icon = new Image(); var iconSource = GetIconSource(image); icon.Source = iconSource; grayIcon = new Image(); FormatConvertedBitmap grayBitmap = new FormatConvertedBitmap(); grayBitmap.BeginInit(); grayBitmap.Source = iconSource; grayBitmap.DestinationFormat = PixelFormats.Gray8; grayIcon.OpacityMask = new ImageBrush(iconSource); grayBitmap.EndInit(); // Set Source property of Image grayIcon.Source = grayBitmap; return(icon); }
//--------------------------------------------------------------------------- /** * @fn public void SetImage(BitmapImage bmpimg, Stretch stretch = Stretch.None) * @brief WriteableBitmap Type을 Align Control에 Set. * @return void * @param BitmapImage bmpimg : Source Image. * @param Stretch stretch : Stretch Option. * @remark * - Stretch Option을 Fill로 할 경우, 배율이 맞지 않을 수 있음. * - 배율 조정 + Fit을 원하면 GetFitScale을 사용하여 수동으로 화면 Scale 조정 할 것. * @author 선경규(Kyeong Kyu - Seon) * @date 2020/3/9 16:40 */ public void SetImage(BitmapImage bmpimg, Stretch stretch = Stretch.None) { WriteableBitmap wbm = null; FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap(); // BitmapSource objects like FormatConvertedBitmap can only have their properties // changed within a BeginInit/EndInit block. newFormatedBitmapSource.BeginInit(); // Use the BitmapSource object defined above as the source for this new // BitmapSource (chain the BitmapSource objects together). newFormatedBitmapSource.Source = bmpimg; // Set the new format to Gray32Float (grayscale). newFormatedBitmapSource.DestinationFormat = PixelFormats.Bgr24; newFormatedBitmapSource.EndInit(); wbm = new WriteableBitmap(newFormatedBitmapSource); m_ImgBrs = new ImageBrush(wbm); m_ImgBrs.Stretch = stretch; if (m_ImgBrs != null) { m_ImgBrsOrg = m_ImgBrs.Clone(); } if (m_ImgBrs.Stretch == Stretch.None) { lib_Canvas.Width = m_ImgBrs.ImageSource.Width; lib_Canvas.Height = m_ImgBrs.ImageSource.Height; } m_nWidth = (int)m_ImgBrs.ImageSource.Width; m_nHeight = (int)m_ImgBrs.ImageSource.Height; m_nChannel = (int)(wbm.Format.BitsPerPixel / 8.0); lib_Canvas.Background = new ImageBrush(wbm); m_dScale = myScaleTransform.ScaleX; }
public static FormatConvertedBitmap ConvertToGrayScale(string fileName) { BitmapImage myBitmapImage = new BitmapImage(); // BitmapSource objects like BitmapImage can only have their properties // changed within a BeginInit/EndInit block. myBitmapImage.BeginInit(); myBitmapImage.UriSource = new Uri(fileName, UriKind.Relative); // To save significant application memory, set the DecodePixelWidth or // DecodePixelHeight of the BitmapImage value of the image source to the desired // height or width of the rendered image. If you don't do this, the application will // cache the image as though it were rendered as its normal size rather then just // the size that is displayed. // Note: In order to preserve aspect ratio, set DecodePixelWidth // or DecodePixelHeight but not both. myBitmapImage.EndInit(); ////////// Convert the BitmapSource to a new format //////////// // Use the BitmapImage created above as the source for a new BitmapSource object // which is set to a gray scale format using the FormatConvertedBitmap BitmapSource. // Note: New BitmapSource does not cache. It is always pulled when required. FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap(); // BitmapSource objects like FormatConvertedBitmap can only have their properties // changed within a BeginInit/EndInit block. newFormatedBitmapSource.BeginInit(); // Use the BitmapSource object defined above as the source for this new // BitmapSource (chain the BitmapSource objects together). newFormatedBitmapSource.Source = myBitmapImage; // Set the new format to Gray32Float (grayscale). newFormatedBitmapSource.DestinationFormat = PixelFormats.Gray32Float; newFormatedBitmapSource.EndInit(); return(newFormatedBitmapSource); }
public void UpdateImage() { try { Uri myUri = new Uri(m_images.m_pathThumb, UriKind.RelativeOrAbsolute); BmpBitmapDecoder decoder = new BmpBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); BitmapSource bi3 = decoder.Frames[0]; // Begin: Fix 61368 if (m_images.m_colorMode == EnumColorType.black_white) { bi3 = BitmapFrame.Create(new TransformedBitmap(bi3, new ScaleTransform(0.1, 0.1))); // https://msdn.microsoft.com/en-us/library/system.windows.media.imaging.formatconvertedbitmap(v=vs.100).aspx FormatConvertedBitmap bmpSrc = new FormatConvertedBitmap(); bmpSrc.BeginInit(); bmpSrc.Source = bi3; bmpSrc.DestinationFormat = PixelFormats.Gray2; bmpSrc.EndInit(); imgBody.Source = bmpSrc; } else { imgBody.Source = bi3; } // End: Fix 61368 this.Background = Brushes.White; this.Width = 105; this.Height = 140; } catch { } m_iSimgReady = (null != imgBody.Source); }
private static ImageSource Convert(BitmapSource input, PixelFormat format) { ////////// Convert the BitmapSource to a new format //////////// // Use the BitmapImage created above as the source for a new BitmapSource object // which is set to a gray scale format using the FormatConvertedBitmap BitmapSource. // Note: New BitmapSource does not cache. It is always pulled when required. FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap(); // BitmapSource objects like FormatConvertedBitmap can only have their properties // changed within a BeginInit/EndInit block. newFormatedBitmapSource.BeginInit(); // Use the BitmapSource object defined above as the source for this new // BitmapSource (chain the BitmapSource objects together). newFormatedBitmapSource.Source = input; // Set the new format to Gray32Float (grayscale). newFormatedBitmapSource.DestinationFormat = format; newFormatedBitmapSource.EndInit(); return(newFormatedBitmapSource); }