Esempio n. 1
0
        public RgbImageAdapter(string path)
        {
            this.source = new BitmapImage(new Uri(path));

            if (source.Format != PixelFormats.Bgra32)
            {
                source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0);
            }

            int width = source.PixelWidth;
            int height = source.PixelHeight;
            RgbPixel[,] result = new RgbPixel[height, width];

            byte[] byteArray = new byte[height * width * 4];
            int stride = width * 4;
            source.CopyPixels(byteArray, stride, 0);
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    result[i, j] = new RgbPixel
                    {
                        Blue = byteArray[(i * width + j) * 4 + 0],
                        Green = byteArray[(i * width + j) * 4 + 1],
                        Red = byteArray[(i * width + j) * 4 + 2],
                        Alpha = byteArray[(i * width + j) * 4 + 3],
                    };
                }
            }

            image = new RgbImage(result);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            List <Image> imagesArray = new System.Collections.Generic.List <Image>();

            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"C:\CosaHelperLib");

            foreach (System.IO.FileInfo file in dir.GetFiles())
            {
                String ext = file.Extension;
                Console.WriteLine(file.FullName);
                if (ext.Equals(".ico"))
                {
                    Stream            iconStream = new FileStream(file.FullName, FileMode.Open);
                    IconBitmapDecoder decoder    = new IconBitmapDecoder(iconStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);

                    foreach (var item in decoder.Frames)
                    {
                        var src = new System.Windows.Media.Imaging.FormatConvertedBitmap();
                        src.BeginInit();
                        src.Source = item;
                        src.EndInit();

                        Console.WriteLine(file.FullName + '_' + src.PixelHeight + 'X' + src.PixelWidth);
                        //Console.WriteLine("Pixel Width: " + src.PixelWidth);
                        //Console.WriteLine("Pixel Height: " + src.PixelHeight);
                        //Console.WriteLine("Pixel Width: " + src.PixelWidth);
                        Console.WriteLine("Alpha Channel Threshold: " + src.AlphaThreshold);
                    }
                }
                Console.WriteLine();
            }
            Console.Read();
        }
        private byte[] RenderCurve(
			ref RenderTargetBitmap rtb,
			ref FormatConvertedBitmap fcb,
			int width,
			IEnumerable<IList<Point>> collection,
			double thickness,
			int miterLimit,
			int height,
			int stride)
        {
            if (rtb == null
                || rtb.PixelWidth != width
                || rtb.PixelHeight != height)
            {
                rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
                fcb = new FormatConvertedBitmap(rtb, DrawingParameter.TargetPixelFormat, DrawingParameter.TargetBitmapPalette, 1);
            }
            else
            {
                rtb.Clear();
            }

            collection
                .Select(it =>
                    {
                        var visual = _drawingVisualFactory();
                        visual.Draw(it, thickness, miterLimit, _logicalToScreenMapperFactory());
                        return visual;
                    })
                .ForEachElement(rtb.Render);

            var curvePixels = new byte[stride * height];
            fcb.CopyPixels(curvePixels, stride, 0);
            return curvePixels;
        }
Esempio n. 4
0
        public void Button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                BitmapImage myBitmapImage = new BitmapImage();
                myBitmapImage.BeginInit();
                myBitmapImage.UriSource = new Uri(@text1.Text);
                myBitmapImage.EndInit();
                FormatConvertedBitmap gray = new FormatConvertedBitmap(myBitmapImage, PixelFormats.Gray4, null, 0);
                myImage.Source = gray;
                var enc = new JpegBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(gray));
                using (var s = File.Create("gray.jpg"))
                    enc.Save(s);
            }
            catch
            {
                text1.Text = "Не верно введен путь";
            }

            /* public void Button2_Click(object sender, RoutedEventArgs e)
             {
                 BitmapImage myBitmapImage = new BitmapImage();
                 myBitmapImage.BeginInit();
                 myBitmapImage.UriSource=new Uri(myImage);
                 myBitmapImage.EndInit();
                 var enc = new JpegBitmapEncoder();
                 enc.Frames.Add(BitmapFrame.Create(gray));
                 using (var s = File.Create("gray.jpg"))
                     enc.Save(s);*/
        }
Esempio n. 5
0
        /// <summary>
        /// Cashes original ImageSource, creates and caches greyscale ImageSource and greyscale opacity mask
        /// </summary>
        private void SetSources()
        {
            _sourceC = Source;

              try
              {
            // create and cache greyscale ImageSource
            _sourceG = new FormatConvertedBitmap(new BitmapImage(new Uri(TypeDescriptor.GetConverter(Source).ConvertTo(Source, typeof(string)) as string)),
                                             PixelFormats.Gray16, null, 0);

            // create Opacity Mask for greyscale image as FormatConvertedBitmap does not keep transparency info
            _opacityMaskG = new ImageBrush(_sourceC);
            _opacityMaskG.Opacity = 0.5;

            this.Source = IsEnabled ? _sourceC : _sourceG;
            this.OpacityMask = IsEnabled ? _opacityMaskC : _opacityMaskG;

            InvalidateProperty(IsEnabledProperty);
              }
              catch
              {
            #if DEBUG
            MessageBox.Show(String.Format("The ImageSource used cannot be greyed out.\nUse BitmapImage or URI as a Source in order to allow greyscaling.\nSource type used: {0}", Source.GetType().Name),
                        "Unsupported Source in GreyableImage", MessageBoxButton.OK, MessageBoxImage.Warning);
            #endif // DEBUG

            // in case greyscale image cannot be created set greyscale source to original Source
            _sourceG = Source;
              }
        }
        public MemoryStream ReduceColorDepth(Bitmap bmp)
        {
            var ms = new MemoryStream();
            bmp.Save(ms, ImageFormat.Png);

            var bi = new BitmapImage();
            bi.BeginInit();
            bi.StreamSource = ms;
            bi.EndInit();

            var newFormatedBitmapSource = new FormatConvertedBitmap();
            newFormatedBitmapSource.BeginInit();

            newFormatedBitmapSource.Source = bi;

            var myPalette = new BitmapPalette(bi, 256);

            newFormatedBitmapSource.DestinationPalette = myPalette;

            //Set PixelFormats
            newFormatedBitmapSource.DestinationFormat = PixelFormats.Indexed8;
            newFormatedBitmapSource.EndInit();

            var pngBitmapEncoder = new PngBitmapEncoder();
            pngBitmapEncoder.Interlace = PngInterlaceOption.Off;
            pngBitmapEncoder.Frames.Add(BitmapFrame.Create(newFormatedBitmapSource));

            var memstream = new MemoryStream();
            pngBitmapEncoder.Save(memstream);
            memstream.Position = 0;
            return memstream;
        }
Esempio n. 7
0
        public static System.Drawing.Bitmap setPixelFormat2(BitmapSource bitmapsource, System.Drawing.Imaging.PixelFormat format)
        {
            //convert image format
            var src = new System.Windows.Media.Imaging.FormatConvertedBitmap();
            src.BeginInit();
            src.Source = bitmapsource;
            if (format == System.Drawing.Imaging.PixelFormat.Format1bppIndexed)
            {
                src.DestinationFormat = System.Windows.Media.PixelFormats.BlackWhite;
            }
            if (format == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
            {
                src.DestinationFormat = System.Windows.Media.PixelFormats.Gray8;
            }
            if (format == System.Drawing.Imaging.PixelFormat.Format24bppRgb)
            {
                src.DestinationFormat = System.Windows.Media.PixelFormats.Bgr24;
            }
            if (format == System.Drawing.Imaging.PixelFormat.Format32bppRgb)
            {
                src.DestinationFormat = System.Windows.Media.PixelFormats.Bgr32;
            }
            src.EndInit();

            //copy to bitmap
            Bitmap bitmap = new Bitmap(src.PixelWidth, src.PixelHeight, format);
            var data = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, format);
            src.CopyPixels(System.Windows.Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
            bitmap.UnlockBits(data);

            return bitmap;
        }
        public static void DrawImage (this ConsoleBuffer @this, ImageSource imageSource, int x, int y, int width, int height)
        {
            var bmp = imageSource as BitmapSource;
            if (bmp == null)
                throw new ArgumentException("Only rendering of bitmap source is supported.");

            @this.OffsetX(ref x).OffsetY(ref y);
            int x1 = x, x2 = x + width, y1 = y, y2 = y + height;
            if ([email protected](ref x1, ref y1, ref x2, ref y2))
                return;

            if (width != bmp.PixelWidth || height != bmp.PixelHeight)
                bmp = new TransformedBitmap(bmp, new ScaleTransform((double)width / bmp.PixelWidth, (double)height / bmp.PixelHeight));
            if (bmp.Format != PixelFormats.Indexed4)
                bmp = new FormatConvertedBitmap(bmp, PixelFormats.Indexed4, BitmapPalettes.Halftone8Transparent, 0.5);

            const int bitsPerPixel = 4;
            int stride = 4 * (bmp.PixelWidth * bitsPerPixel + 31) / 32;
            byte[] bytes = new byte[stride * bmp.PixelHeight];
            bmp.CopyPixels(bytes, stride, 0);

            for (int iy = y1, py = 0; iy < y2; iy++, py++) {
                ConsoleChar[] charsLine = @this.GetLine(iy);
                for (int ix = x1, px = 0; ix < x2; ix++, px++) {
                    int byteIndex = stride * py + px / 2;
                    int bitOffset = px % 2 == 0 ? 4 : 0;
                    SetColor(ref charsLine[ix], bmp.Palette, (bytes[byteIndex] >> bitOffset) & 0xF);
                }
            }
        }
Esempio n. 9
0
        unsafe bool Close()
        {
            CloseGroup();
            if (image != null)
            {
                Control.Close();
                var handler = (BitmapHandler)image.Handler;
                var bmp     = image.ToWpf();
                var newbmp  = new swmi.RenderTargetBitmap(bmp.PixelWidth, bmp.PixelHeight, bmp.DpiX, bmp.DpiY, swm.PixelFormats.Pbgra32);
                newbmp.RenderWithCollect(visual);
                if (!bmp.Format.HasAlpha())
                {
                    // convert to non-alpha, as RenderTargetBitmap does not support anything other than Pbgra32
                    var converted = new swmi.FormatConvertedBitmap(newbmp, swm.PixelFormats.Bgr32, null, 0);
                    handler.SetBitmap(converted);

                    // var wb = new swmi.WriteableBitmap(bmp.PixelWidth, bmp.PixelHeight, bmp.DpiX, bmp.DpiY, swm.PixelFormats.Bgr32, null);
                    // var rect = new sw.Int32Rect(0, 0, bmp.PixelWidth, bmp.PixelHeight);
                    // var pixels = new byte[bmp.PixelHeight * wb.BackBufferStride];
                    // newbmp.CopyPixels(rect, pixels, wb.BackBufferStride, 0);
                    // fixed (byte* ptr = pixels)
                    // {
                    //  wb.WritePixels(rect, (IntPtr)ptr, pixels.Length, wb.BackBufferStride, 0, 0);
                    // }
                    // handler.SetBitmap(wb);
                }
                else
                {
                    handler.SetBitmap(newbmp);
                }
                return(true);
            }
            return(false);
        }
        public static bool[,] Rasterize(Point[] points, int width, int height)
        {
            Contract.Requires(points != null);
            Contract.Requires(width > 0);
            Contract.Requires(height > 0);
            Contract.Requires(width % 8 == 0);
            Contract.Ensures(Contract.Result<bool[,]>() != null);
            Contract.Ensures(Contract.Result<bool[,]>().GetLength(0) == width);
            Contract.Ensures(Contract.Result<bool[,]>().GetLength(1) == height);

            var canvas = new Canvas { Background = Brushes.White, Width = width, Height = height };
            var polygon = new Polygon { Stroke = Brushes.Black, Fill = Brushes.Black, StrokeThickness = 1, Points = new PointCollection(points) };
            canvas.Children.Add(polygon);
            RenderOptions.SetEdgeMode(canvas, EdgeMode.Aliased);

            canvas.Measure(new Size(width, height));
            canvas.Arrange(new Rect(0, 0, canvas.DesiredSize.Width, canvas.DesiredSize.Height));

            var rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
            rtb.Render(canvas);

            var fmb = new FormatConvertedBitmap(rtb, PixelFormats.BlackWhite, null, 0);
            var pixels = new byte[width * height / 8];
            fmb.CopyPixels(pixels, width / 8, 0);

            System.Collections.BitArray ba = new System.Collections.BitArray(pixels);

            var result = new bool[width, height];
            for (int i = 0, y = 0; y < height; ++y)
                for (int x = 0; x < width; ++x, ++i)
                    result[x, y] = !ba[i];

            return result;
        }
Esempio n. 11
0
        public override Stream OpenEntry(ArcFile arc, Entry entry)
        {
            var pent = entry as PnaEntry;
            if (null == pent)
                return base.OpenEntry (arc, entry);
            ImageData image;
            using (var input = arc.File.CreateStream (entry.Offset, entry.Size))
                image = ImageFormat.Read (input);
            var bitmap = image.Bitmap;
            if (bitmap.Format.BitsPerPixel != 32)
            {
                bitmap = new FormatConvertedBitmap (bitmap, PixelFormats.Bgra32, null, 0);
            }
            int stride = bitmap.PixelWidth * 4;
            var pixels = new byte[stride * bitmap.PixelHeight];
            bitmap.CopyPixels (pixels, stride, 0);

            // restore colors premultiplied by alpha
            for (int i = 0; i < pixels.Length; i += 4)
            {
                int alpha = pixels[i+3];
                if (alpha != 0 && alpha != 0xFF)
                {
                    pixels[i]   = (byte)(pixels[i]   * 0xFF / alpha);
                    pixels[i+1] = (byte)(pixels[i+1] * 0xFF / alpha);
                    pixels[i+2] = (byte)(pixels[i+2] * 0xFF / alpha);
                }
            }
            return TgaStream.Create (pent.Info, pixels);
        }
Esempio n. 12
0
 public MyImageReader(string fileName)
 {
     var filePath = Path.Combine(Environment.CurrentDirectory, fileName);
     _bitmap = new FormatConvertedBitmap(new BitmapImage(new Uri("file://" + filePath)), System.Windows.Media.PixelFormats.Bgr32, null, 0);
     _pixels = new uint[_bitmap.PixelHeight * _bitmap.PixelWidth];
     _bitmap.CopyPixels(_pixels, sizeof(uint) * _bitmap.PixelWidth, 0);
 }
Esempio n. 13
0
        internal void SetIcon(string fileName)
        {
            var bitmapSource = new BitmapImage();

            bitmapSource.BeginInit();
            bitmapSource.UriSource = new Uri(fileName);

            // ensure that we resize to the correct dimensions
            bitmapSource.DecodePixelHeight = 46;
            bitmapSource.DecodePixelWidth = 46;

            bitmapSource.EndInit();

            var pbgra32Image = new FormatConvertedBitmap(bitmapSource, System.Windows.Media.PixelFormats.Pbgra32, null, 0);

            var bmp = new System.Windows.Media.Imaging.WriteableBitmap(pbgra32Image);

            var images = new List<BandIcon>() { bmp.ToBandIcon(), bmp.ToBandIcon() };

            Strapp.SetImageList(Strapp.TileId, images, 0);

            _tiles.UpdateStrapp(Strapp);

            // this should refresh all bindings into the Strapp (in particular, the image)
            NotifyPropertyChanged("Strapp");
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="BitmapSourceLuminanceSource"/> class.
 /// </summary>
 /// <param name="bitmap">The bitmap.</param>
 public BitmapSourceLuminanceSource(BitmapSource bitmap)
    : base(bitmap.PixelWidth, bitmap.PixelHeight)
 {
    switch (bitmap.Format.ToString())
    {
       case "Bgr24":
       case "Bgr32":
          CalculateLuminanceBGR(bitmap);
          break;
       case "Bgra32":
          CalculateLuminanceBGRA(bitmap);
          break;
       case "Rgb24":
          CalculateLuminanceRGB(bitmap);
          break;
       case "Bgr565":
          CalculateLuminanceBGR565(bitmap);
          break;
       default:
          // there is no special conversion routine to luminance values
          // we have to convert the image to a supported format
          bitmap = new FormatConvertedBitmap(bitmap, PixelFormats.Bgra32, null, 0);
          CalculateLuminanceBGR(bitmap);
          break;
    }
 }
Esempio n. 15
0
        public static System.Drawing.Bitmap setPixelFormat2(BitmapSource bitmapsource, System.Drawing.Imaging.PixelFormat format)
        {
            //convert image format
            var src = new System.Windows.Media.Imaging.FormatConvertedBitmap();

            src.BeginInit();
            src.Source = bitmapsource;
            if (format == System.Drawing.Imaging.PixelFormat.Format1bppIndexed)
            {
                src.DestinationFormat = System.Windows.Media.PixelFormats.BlackWhite;
            }
            if (format == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
            {
                src.DestinationFormat = System.Windows.Media.PixelFormats.Gray8;
            }
            if (format == System.Drawing.Imaging.PixelFormat.Format24bppRgb)
            {
                src.DestinationFormat = System.Windows.Media.PixelFormats.Bgr24;
            }
            if (format == System.Drawing.Imaging.PixelFormat.Format32bppRgb)
            {
                src.DestinationFormat = System.Windows.Media.PixelFormats.Bgr32;
            }
            src.EndInit();

            //copy to bitmap
            Bitmap bitmap = new Bitmap(src.PixelWidth, src.PixelHeight, format);
            var    data   = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, format);

            src.CopyPixels(System.Windows.Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
            bitmap.UnlockBits(data);

            return(bitmap);
        }
Esempio n. 16
0
        public static BitmapSource CreateThemedBitmapSource(BitmapSource src, Color bgColor, bool isHighContrast)
        {
            unchecked {
                if (src.Format != PixelFormats.Bgra32)
                    src = new FormatConvertedBitmap(src, PixelFormats.Bgra32, null, 0.0);
                var pixels = new byte[src.PixelWidth * src.PixelHeight * 4];
                src.CopyPixels(pixels, src.PixelWidth * 4, 0);

                var bg = HslColor.FromColor(bgColor);
                int offs = 0;
                while (offs + 4 <= pixels.Length) {
                    var hslColor = HslColor.FromColor(Color.FromRgb(pixels[offs + 2], pixels[offs + 1], pixels[offs]));
                    double hue = hslColor.Hue;
                    double saturation = hslColor.Saturation;
                    double luminosity = hslColor.Luminosity;
                    double n1 = Math.Abs(0.96470588235294119 - luminosity);
                    double n2 = Math.Max(0.0, 1.0 - saturation * 4.0) * Math.Max(0.0, 1.0 - n1 * 4.0);
                    double n3 = Math.Max(0.0, 1.0 - saturation * 4.0);
                    luminosity = TransformLuminosity(hue, saturation, luminosity, bg.Luminosity);
                    hue = hue * (1.0 - n3) + bg.Hue * n3;
                    saturation = saturation * (1.0 - n2) + bg.Saturation * n2;
                    if (isHighContrast)
                        luminosity = ((luminosity <= 0.3) ? 0.0 : ((luminosity >= 0.7) ? 1.0 : ((luminosity - 0.3) / 0.4))) * (1.0 - saturation) + luminosity * saturation;
                    var color = new HslColor(hue, saturation, luminosity, 1.0).ToColor();
                    pixels[offs + 2] = color.R;
                    pixels[offs + 1] = color.G;
                    pixels[offs] = color.B;
                    offs += 4;
                }

                BitmapSource newImage = BitmapSource.Create(src.PixelWidth, src.PixelHeight, src.DpiX, src.DpiY, PixelFormats.Bgra32, src.Palette, pixels, src.PixelWidth * 4);
                newImage.Freeze();
                return newImage;
            }
        }
Esempio n. 17
0
 public static BitmapImage ConvertToOtherPixelFormat(BitmapSource source, PixelFormat format)
 {
     var newFormatedBitmapSource = new FormatConvertedBitmap();
     newFormatedBitmapSource.BeginInit();
     newFormatedBitmapSource.Source = source;
     newFormatedBitmapSource.DestinationFormat = format;
     newFormatedBitmapSource.EndInit();
     return BitmapSourceToBitmapImage(newFormatedBitmapSource.Source);
 }
Esempio n. 18
0
        public static FormatConvertedBitmap GetFormatConvertedBitmap(SWM.PixelFormat pf, BitmapSource bs)
        {
            FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap();
            newFormatedBitmapSource.BeginInit();
            newFormatedBitmapSource.Source = bs;
            newFormatedBitmapSource.DestinationFormat = pf;
            newFormatedBitmapSource.EndInit();

            return newFormatedBitmapSource;
        }
Esempio n. 19
0
        private static BitmapSource LoadBitmap(string path)
        {
            BitmapSource bitmapSource = new BitmapImage(new Uri(Path.GetFullPath(path)));

            if (bitmapSource.Format != workingFormat)
            {
                bitmapSource = new FormatConvertedBitmap(bitmapSource, workingFormat, null, 0.5);
            }

            return bitmapSource;
        }
Esempio n. 20
0
        public static Bitmap ToGdiBitmap(BitmapSource bmp)
        {
            // below code refernces blog entry by luche:
            // http://www.ruche-home.net/%A4%DC%A4%E4%A4%AD%A4%B4%A4%C8/2011-08-02/CopyPixels%20%A5%E1%A5%BD%A5%C3%A5%C9%A4%F2%CD%D1%A4%A4%A4%BF%20WPF%20BitmapSource%20%A4%AB%A4%E9%20GDI%20Bitmap%20%A4%D8%A4%CE%CA%D1%B4%B9

            if (bmp.Format != PixelFormats.Bgra32)
            {
                // convert format
                bmp = new FormatConvertedBitmap(bmp,
                    PixelFormats.Bgra32, null, 0);
                bmp.Freeze();
            }

            // prepare values
            var width = (int)bmp.Width;
            var height = (int)bmp.Height;
            var stride = width * 4;

            // prepare buffer
            var buffer = new byte[stride * height];

            // copy to byte array
            bmp.CopyPixels(buffer, stride, 0);

            // create bitmap
            var result = new Bitmap(width, height, SDI::PixelFormat.Format32bppArgb);

            // set bitmap content
            try
            {
                // locking bits
                SDI::BitmapData bd = null;
                try
                {
                    bd = result.LockBits(new Rectangle(0, 0, width, height),
                        SDI::ImageLockMode.WriteOnly, SDI::PixelFormat.Format32bppArgb);
                    Marshal.Copy(buffer, 0, bd.Scan0, buffer.Length);
                }
                finally
                {
                    if (bd != null)
                    {
                        result.UnlockBits(bd);
                    }
                }
            }
            catch
            {
                result.Dispose();
                throw;
            }

            return result;
        }
Esempio n. 21
0
        /// <summary>
        /// Converts a bitmap and returns the result.
        /// </summary>
        /// <param name="bitmap">The bitmap to convert.</param>
        /// <returns>The result.</returns>
        public BitmapSource Convert(BitmapSource bitmap)
        {
            BitmapSource result = bitmap;

            if(bitmap.Format != _format)
                result = new FormatConvertedBitmap(result, _format, null, 0);

            if(Rotate != 0)
                result = new TransformedBitmap(result, new RotateTransform(Rotate));

            return result;
        }
        public ShowMyFace()
        {
            Title = "Show My Face";

            ///******************************************************************
            //  3���� ShowMyFace ����
            //******************************************************************/
            Uri uri = new Uri("http://www.charlespetzold.com/PetzoldTattoo.jpg");
            //BitmapImage bitmap = new BitmapImage(uri);
            //Image img = new Image();
            //img.Source = bitmap;
            //Content = img;

            ///******************************************************************
            //  p1245 BitmapImage �ڵ�
            //******************************************************************/
            Image rotated90 = new Image();
            TransformedBitmap tb = new TransformedBitmap();
            FormatConvertedBitmap fb = new FormatConvertedBitmap();
            CroppedBitmap cb = new CroppedBitmap();

            // Create the source to use as the tb source.
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = uri;
            bi.EndInit();

            //cb.BeginInit();
            //cb.Source = bi;
            //Int32Rect rect = new Int32Rect();
            //rect.X = 220;
            //rect.Y = 200;
            //rect.Width = 120;
            //rect.Height = 80;
            //cb.SourceRect = rect;

            //cb.EndInit();

            fb.BeginInit();
            fb.Source = bi;
            fb.DestinationFormat = PixelFormats.Gray2;
            fb.EndInit();

            // Properties must be set between BeginInit and EndInit calls.
            tb.BeginInit();
            tb.Source = fb;
            // Set image rotation.
            tb.Transform = new RotateTransform(90);
            tb.EndInit();
            // Set the Image source.
            rotated90.Source = tb;
            Content = rotated90;
        }
Esempio n. 23
0
        private static void SetGrayscale(System.Windows.Controls.Image img)
        {
            img.IsEnabled = false;

            FormatConvertedBitmap bitmap = new FormatConvertedBitmap();
            bitmap.BeginInit();
            bitmap.Source = (BitmapSource)img.Source;
            bitmap.DestinationFormat = PixelFormats.Gray32Float;
            bitmap.EndInit();

            img.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() => { img.Source = bitmap; }));
        }
Esempio n. 24
0
        public static PixelColor[,] GetPixels(BitmapSource source)
        {
            if (source.Format != PixelFormats.Bgra32)
                source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0);

            int width = source.PixelWidth;
            int height = source.PixelHeight;
            PixelColor[,] result = new PixelColor[width, height];

            BitmapSourceHelper.CopyPixels(source, result, width * 4, 0);
            return result;
        }
Esempio n. 25
0
        public PixelBitmap(BitmapSource source)
        {
            if (source.Format != PixelFormats.Bgra32)
            {
                source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0);
            }
            this.Width = source.PixelWidth;
            this.Height = source.PixelHeight;

            this.Pixels = new PixelColor[this.Width * this.Height];
            this.copyPixels(source, this.Pixels, this.Width * 4, 0);
        }
        /// <summary>
        /// Convert bmpSource to format declared in pixFormat.
        /// </summary>
        /// <param name="bmpSource"></param>
        /// <param name="pixFormat"></param>
        /// <returns></returns>
        public static FormatConvertedBitmap BitmapToFormat(BitmapSource bmpSource,
                                           PixelFormat pixFormat)
        {
            if (bmpSource == null) { return null; }
            FormatConvertedBitmap fcb = new FormatConvertedBitmap();

            fcb.BeginInit();
            fcb.Source = bmpSource;
            fcb.DestinationFormat = pixFormat;
            fcb.EndInit();
            return fcb;
        }
Esempio n. 27
0
        public static void CombineTiffs(string DestinationPath, IEnumerable<string> FilePathToMerge, FileMode DestinationPathMode)
        {
            //create the file stream to write to the file
            using (FileStream FileStreamToUse = new FileStream(DestinationPath, DestinationPathMode))
            {
                //create the tiff encoder
                var Encoder = new TiffBitmapEncoder();

                //set the compression
                Encoder.Compression = TiffCompressOption.Ccitt4;

                //loop through each of the files to combine
                foreach (string FileNameToMerge in FilePathToMerge)
                {
                    try
                    {
                        //declare the bitmap image
                        var ImageToMerge = new BitmapImage();

                        //start the process
                        ImageToMerge.BeginInit();

                        //do everything on load
                        ImageToMerge.CacheOption = BitmapCacheOption.OnLoad;

                        //set the file source
                        ImageToMerge.UriSource = new Uri(FileNameToMerge);

                        //we are done and ready to convert
                        ImageToMerge.EndInit();

                        //convert it now
                        FormatConvertedBitmap ConvertedBitMap = new FormatConvertedBitmap(ImageToMerge,
                                                                                          PixelFormats.BlackWhite,
                                                                                          BitmapPalettes.BlackAndWhite,
                                                                                          1.0);

                        //create the frames
                        Encoder.Frames.Add(BitmapFrame.Create(ConvertedBitMap));
                    }

                    catch (NotSupportedException)
                    {
                        //ignore these exception? (means bad image file...or is not a tiff file)
                        throw;
                    }
                }

                //save the file
                Encoder.Save(FileStreamToUse);
            }
        }
      public void RGB565LuminanceSource_Should_Give_The_Same_Result_As_RGBLuminanceSource_For_BGR565()
      {
         BitmapSource bitmapImage = new BitmapImage(new Uri(samplePicRelPath, UriKind.RelativeOrAbsolute));
         bitmapImage = new FormatConvertedBitmap(bitmapImage, PixelFormats.Bgr565, null, 0);
         var bytes = new byte[bitmapImage.PixelHeight*bitmapImage.PixelWidth*2];
         bitmapImage.CopyPixels(bytes, bitmapImage.PixelWidth * 2, 0);

         var rgb565LuminanceSource = new RGB565LuminanceSource(bytes, bitmapImage.PixelWidth, bitmapImage.PixelHeight);

         var rgbLuminanceSource = new BitmapSourceLuminanceSource(bitmapImage);

         Assert.AreEqual(rgbLuminanceSource.ToString(), rgb565LuminanceSource.ToString());
      }
        public ImageSource GetCroppedImage(BitmapSource source, int cropTop, int cropBottom, int cropLeft, int cropRight)
        {
            if (source == null)
                return null;

            if (source.Format != PixelFormats.Bgra32)
                source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0);

            if (cropRight > source.Width || cropBottom > source.Height)
                return null;

            return new CroppedBitmap(source, new Int32Rect(cropLeft, cropTop, cropRight - cropLeft, cropBottom - cropTop));
        }
Esempio n. 30
0
        public static RgbaBitmapBuffer FromBitmapSource(BitmapSource bitmapSource)
        {
            if (bitmapSource.Format != workingFormat)
            {
                bitmapSource = new FormatConvertedBitmap(bitmapSource, workingFormat, null, 0.5);
            }

            var floatStride = workingChannelCount * bitmapSource.PixelWidth;
            var buffer = new float[floatStride * bitmapSource.PixelHeight];

            bitmapSource.CopyPixels(buffer, sizeof(float) * floatStride, 0);

            return new RgbaBitmapBuffer(buffer, floatStride);
        }
Esempio n. 31
0
        public static PixelColor[,] GetPixels(this BitmapSource source)
        {
            if (source.Format != PixelFormats.Bgra32)
                source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0);

            int width = source.PixelWidth;
            int height = source.PixelHeight;
            PixelColor[,] result = new PixelColor[width,height];

            // ReSharper disable RedundantNameQualifier
            // qualifier is NOT redudant, in fact if you don't have it we crash.
            PixelRetrievalExtensions.CopyPixels(source,result, width * 4, 0);
            // ReSharper restore RedundantNameQualifier
            return result;
        }
Esempio n. 32
0
        /// <summary>
        /// Extension method
        /// </summary>
        /// <param name="bitmapsource"></param>
        /// <returns></returns>
        public static Bitmap BitmapSourceToBitmap(this BitmapSource bitmapsource) {
            //convert image format
            FormatConvertedBitmap src = new FormatConvertedBitmap();
            src.BeginInit();
            src.Source = bitmapsource;
            src.DestinationFormat = System.Windows.Media.PixelFormats.Bgra32;
            src.EndInit();

            //copy to bitmap
            Bitmap bitmap = new Bitmap(src.PixelWidth, src.PixelHeight, PixelFormat.Format32bppArgb);
            BitmapData data = bitmap.LockBits(new Rectangle(System.Drawing.Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
            src.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height*data.Stride, data.Stride);
            bitmap.UnlockBits(data);

            return bitmap;
        }
        public static void Store(FrameworkElement fwElement, string fileName)
        {
            var width = (int)fwElement.ActualWidth;
            var height = (int)fwElement.ActualHeight;
            var rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
            rtb.Render(fwElement);

            var bwImage = new FormatConvertedBitmap(rtb, PixelFormats.BlackWhite, null, 0);

            var encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bwImage));
            using (var stream = File.Create(fileName))
            {
                encoder.Save(stream);
            }
        }
Esempio n. 34
0
        public static System.Drawing.Bitmap BmpSource2Img(BitmapSource bmpsource)
        {
            //convert image format
            var src = new System.Windows.Media.Imaging.FormatConvertedBitmap();

            src.BeginInit();
            src.Source = bmpsource;
            src.EndInit();

            //copy to bitmap
            Bitmap bitmap = new Bitmap(src.PixelWidth, src.PixelHeight, System.Drawing.Imaging.PixelFormat.DontCare);
            var    data   = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.DontCare);

            src.CopyPixels(System.Windows.Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
            bitmap.UnlockBits(data);

            return(bitmap);
        }
        public Bitmap BitmapFromSource(System.Windows.Media.Imaging.BitmapSource bitmapsource)
        {
            //convert image format
            var src = new System.Windows.Media.Imaging.FormatConvertedBitmap();

            src.BeginInit();
            src.Source            = bitmapsource;
            src.DestinationFormat = System.Windows.Media.PixelFormats.Bgra32;
            src.EndInit();

            //copy to bitmap
            Bitmap bitmap = new Bitmap(src.PixelWidth, src.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            var    data   = bitmap.LockBits(new Rectangle(System.Drawing.Point.Empty, bitmap.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            src.CopyPixels(System.Windows.Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
            bitmap.UnlockBits(data);

            return(bitmap);
        }
Esempio n. 36
0
 private void ClonePrequel(FormatConvertedBitmap otherFormatConvertedBitmap)
 {
     BeginInit();
 }
Esempio n. 37
0
 private void ClonePostscript(FormatConvertedBitmap otherFormatConvertedBitmap)
 {
     EndInit();
 }