public static WriteableBitmap Overlay(WriteableBitmap bmp, WriteableBitmap overlay, Point location)
 {
     var result = bmp.Clone();
     var size = new Size(overlay.PixelWidth, overlay.PixelHeight);
     result.Blit(new Rect(location, size), overlay, new Rect(new Point(0, 0), size), WriteableBitmapExtensions.BlendMode.Alpha);
     return result;
 }
Example #2
0
        /// <summary>
        /// Applies a watermark bitmap and returns a new processed WriteabelBitmap.
        /// </summary>
        /// <param name="input">The input bitmap.</param>
        /// <returns>The result WriteableBitmap with the watermark.</returns>
        public WriteableBitmap Apply(WriteableBitmap input)
        {
            // Resize watermark
            var w = Watermark.PixelWidth;
            var h = Watermark.PixelHeight;
            var ratio = (float)w / h;
            if (ratio > 1)
            {
                w = (int)(input.PixelWidth * RelativeSize);
                h = (int)(w / ratio);
            }
            else
            {
                h = (int)(input.PixelHeight * RelativeSize);
                w = (int)(h * ratio);
            }
            var watermark = Watermark.Resize(w, h, WriteableBitmapExtensions.Interpolation.Bilinear);

            // Blit watermark into copy of the input
            // Bottom right corner
            var result = input.Clone();
            var position = new Rect(input.PixelWidth - w, input.PixelHeight - h, w, h);
            result.Blit(position, watermark, new Rect(0, 0, w, h));

            return result;
        }
Example #3
0
        /// <summary>
        /// Rotates the bitmap in 90° steps clockwise and returns a new rotated WriteableBitmap.
        /// </summary>
        /// <param name="bmp">The WriteableBitmap.</param>
        /// <param name="angle">The angle in degress the bitmap should be rotated in 90° steps clockwise.</param>
        /// <returns>A new WriteableBitmap that is a rotated version of the input.</returns>
        public static WriteableBitmap Rotate(this WriteableBitmap bmp, int angle)
        {
            // Use refs for faster access (really important!) speeds up a lot!
            var             w      = bmp.PixelWidth;
            var             h      = bmp.PixelHeight;
            var             p      = bmp.Pixels;
            var             i      = 0;
            WriteableBitmap result = null;

            angle %= 360;

            if (angle > 0 && angle <= 90)
            {
                result = new WriteableBitmap(h, w);
                var rp = result.Pixels;
                for (var x = 0; x < w; x++)
                {
                    for (var y = h - 1; y >= 0; y--)
                    {
                        var srcInd = y * w + x;
                        rp[i] = p[srcInd];
                        i++;
                    }
                }
            }
            else if (angle > 90 && angle <= 180)
            {
                result = new WriteableBitmap(w, h);
                var rp = result.Pixels;
                for (var y = h - 1; y >= 0; y--)
                {
                    for (var x = w - 1; x >= 0; x--)
                    {
                        var srcInd = y * w + x;
                        rp[i] = p[srcInd];
                        i++;
                    }
                }
            }
            else if (angle > 180 && angle <= 270)
            {
                result = new WriteableBitmap(h, w);
                var rp = result.Pixels;
                for (var x = w - 1; x >= 0; x--)
                {
                    for (var y = 0; y < h; y++)
                    {
                        var srcInd = y * w + x;
                        rp[i] = p[srcInd];
                        i++;
                    }
                }
            }
            else
            {
                result = bmp.Clone();
            }
            return(result);
        }
Example #4
0
        public Octree(WriteableBitmap wbm)
        {
            LoadedBitmap = wbm.Clone();

            InitializeAllLevelsArray();
            GenerateOctreeForBitmap(wbm);

            if(Root != null)
            {
                Root.Parent = null;
            }
        }
 public Image(WriteableBitmap img)
 {
     this.image = img.Clone();
     formatName = "jpg";
     width = img.PixelWidth;
     height = img.PixelHeight;
     //img.Dispatcher.BeginInvoke(() =>
        // {
         destImage = new WriteableBitmap(width, height);
     //});
     updateColorArray();
 }
        /// <summary>
        /// 画像に遺影を重ねます。
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        private WriteableBitmap ApplyYey(WriteableBitmap bitmap)
        {
            var wb = bitmap.Clone();
            var file = Application.GetResourceStream(new Uri("photoframe.png", UriKind.Relative));

            var yey = new WriteableBitmap(wb.PixelWidth, wb.PixelHeight);
            yey.LoadJpeg(file.Stream);

            for (int i = 0; i < yey.Pixels.Length; i++)
            {
                var oPx = yey.Pixels[i];
                var a2 = Convert.ToByte((oPx >> 24) & 0xff);
                var ax2 = (double)a2 / 255;

                if (a2 == 0)
                {
                    // 透明なので下地を描画
                }
                else if (a2 == 255)
                {
                    // 不透明なので上に書き込み
                    wb.Pixels[i] = yey.Pixels[i];
                }
                else
                {
                    // 下地の画像の色を取り出す
                    var a = Convert.ToByte((wb.Pixels[i] >> 24) & 0xff);
                    var r = Convert.ToByte((wb.Pixels[i] >> 16) & 0xff);
                    var g = Convert.ToByte((wb.Pixels[i] >> 8) & 0xff);
                    var b = Convert.ToByte((wb.Pixels[i]) & 0xff);
                    // 枠の色を取り出す
                    var r2 = Convert.ToByte((oPx >> 16) & 0xff);
                    var g2 = Convert.ToByte((oPx >> 8) & 0xff);
                    var b2 = Convert.ToByte((oPx) & 0xff);

                    // アルファ値を元に合成
                    var a3 = Convert.ToByte((a * (1.0 - ax2) + a2 * ax2));
                    var r3 = Convert.ToByte((r * (1.0 - ax2) + r2 * ax2));
                    var g3 = Convert.ToByte((g * (1.0 - ax2) + g2 * ax2));
                    var b3 = Convert.ToByte((b * (1.0 - ax2) + b2 * ax2));

                    wb.Pixels[i] = a3 << 24 | r3 << 16 | g3 << 8 | b3;
                }
            }

            wb.Invalidate();
            return wb;
        }
        public void LoadImage(WriteableBitmap bitmap, Action<WriteableBitmap, short, short> drawer)
        {
            Image.Source = bitmap;
            MinSlider.ValueChanged += (o, e) =>
            {
                drawer(bitmap, (short)MinSlider.Value, (short)MaxSlider.Value);
            };
            MaxSlider.ValueChanged += (o, e) =>
            {
                drawer(bitmap, (short)MinSlider.Value, (short)MaxSlider.Value);
            };
            drawer(bitmap, (short)MinSlider.Value, (short)MaxSlider.Value);
            SaveRangeButton.Click += (o, e) =>
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.AddExtension = true;
                sfd.FileName = DateTime.Now.ToString("ddMMyyhhmm");
                sfd.DefaultExt = "png";
                sfd.Filter = "Image files (*.png)|*.png";
                if (sfd.ShowDialog() == true)
                {
                    for (short i = 1000; i <= 18000; i += 500)
                    {
                        var clone = bitmap.Clone();
                        short from = (short)(i - 1000);
                        short to = i;
                        drawer(clone, from, to);
                        using (FileStream stream5 = new FileStream(System.IO.Path.GetDirectoryName(sfd.FileName) + "\\" + System.IO.Path.GetFileNameWithoutExtension(sfd.FileName) + string.Format("{0}-{1}", from, to) + ".png", FileMode.Create))
                        {
                            PngBitmapEncoder encoder5 = new PngBitmapEncoder();
                            encoder5.Frames.Add(BitmapFrame.Create(clone));
                            encoder5.Save(stream5);
                            stream5.Close();
                        }
                    }

                }
            };
        }
        /// <summary>
        /// Takes a bitmap whites out all the pixels of a color not of the given color
        /// </summary>
        /// <param name="bitmap">The bitmap to start with</param>
        /// <param name="color">The color to keep</param>
        /// <returns>A bitmap that only has the given color remaining</returns>
        public static WriteableBitmap EraseAllButCertainColor( WriteableBitmap bitmap, Color color )
        {
            int stride = ( bitmap.PixelWidth * bitmap.Format.BitsPerPixel + 7 ) / 8;

             byte[] pixelByteArray = new byte[bitmap.PixelHeight * stride];
             byte[] newPixelByteArray = new byte[bitmap.PixelHeight * stride];
             bitmap.CopyPixels( pixelByteArray, stride, 0 );

             for ( int i = 0; i < bitmap.PixelWidth; i++ )
             {
            for ( int j = 0; j < bitmap.PixelHeight; j++ )
            {
               int index = j * stride + 4 * i;
               if ( GetPixelColor( pixelByteArray, index ) != color )
               {
                  newPixelByteArray[index] = 255;
                  newPixelByteArray[index + 1] = 255;
                  newPixelByteArray[index + 2] = 255;
               }
               else
               {
                  newPixelByteArray[index] = color.B;
                  newPixelByteArray[index + 1] = color.G;
                  newPixelByteArray[index + 2] = color.R;
               }
            }
             }
             var newBitmap = bitmap.Clone();
             newBitmap.WritePixels( new Int32Rect( 0, 0, bitmap.PixelWidth, bitmap.PixelHeight ), newPixelByteArray, stride, 0 );

             return newBitmap;
        }
        /// <summary>
        /// 画像をグレースケールにします。
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        private WriteableBitmap Grayscare(WriteableBitmap bitmap)
        {
            var edit = bitmap.Clone();

            edit.ToGrayScale();

            return edit;

            //using (var s = PictureExtension.PhotoStreamToMemoryStream(bitmap))
            //{
            //    var size = PictureExtension.GetOriginalSize(s);
            //    WriteableBitmap edit = null;

            //    this.originBitmap = new WriteableBitmap((int)size.Width, (int)size.Height);
            //    edit = new WriteableBitmap((int)size.Width, (int)size.Height);

            //    // WriteableBitmapに画像を読み込ませる
            //    s.Seek(0, SeekOrigin.Begin);
            //    this.originBitmap.LoadJpeg(s);
            //    // editableの方にも同じのをコピー
            //    edit = this.originBitmap.Clone();

            //    // グレスケ化
            //    edit = this.originBitmap.ToGrayScale();

            //    return edit;
            //}
        }
Example #10
0
        public static WriteableBitmap UniformQuantization(WriteableBitmap wbm, byte nR, byte nG, byte nB)
        {
            var result = wbm.Clone();

            unsafe
            {
                using (var context = result.GetBitmapContext())
                {
                    for (int i = 0; i < wbm.PixelWidth; i++)
                    {
                        for (int j = 0; j < wbm.PixelHeight; j++)
                        {
                            var c = context.Pixels[j * context.Width + i];
                            byte a = (byte)(c >> 24);

                            // Prevent division by zero
                            int ai = a;
                            if (ai == 0)
                            {
                                ai = 1;
                            }

                            ai = ((255 << 8) / ai);

                            var color = Color.FromArgb(
                                a,
                                (byte)((((c >> 16) & 0xFF) * ai) >> 8),
                                (byte)((((c >> 8) & 0xFF) * ai) >> 8),
                                (byte)((((c & 0xFF) * ai) >> 8)));

                            var r = ReducePixelUq(nR, color.R);
                            var g = ReducePixelUq(nG, color.G);
                            var b = ReducePixelUq(nB, color.B);

                            context.Pixels[j * context.Width + i] = (a << 24) | (r << 16) | (g << 8) | b;
                        }
                    }
                }

                return result;
            }
        }
Example #11
0
        public static WriteableBitmap PopularityAlgorithm(WriteableBitmap wbm, int k)
        {
            var clone = wbm.Clone();

            unsafe
            {
                using (var context = clone.GetBitmapContext())
                {
                    var colorsArray = GetMostPopularColors(context, k).ToArray();

                    if (colorsArray != null)
                    {
                        for (int i = 0; i < context.Width; i++)
                        {
                            for (int j = 0; j < context.Height; j++)
                            {
                                var c = context.Pixels[j * context.Width + i];
                                var a = (byte)(c >> 24);

                                int ai = a;
                                if (ai == 0)
                                {
                                    ai = 1;
                                }

                                ai = ((255 << 8) / ai);
                                var color = Color.FromArgb(a,
                                    (byte)((((c >> 16) & 0xFF) * ai) >> 8),
                                    (byte)((((c >> 8) & 0xFF) * ai) >> 8),
                                    (byte)((((c & 0xFF) * ai) >> 8)));

                                var closestColor = GetTheClosestPixel(color, colorsArray);

                                context.Pixels[j * context.Width + i] = (255 << 24) | (closestColor.R << 16) | (closestColor.G << 8) | closestColor.B;
                            }
                        }
                    }
                }

                return clone;
            }
        }
Example #12
0
        public static WriteableBitmap OctreeAlgorithm(WriteableBitmap wbm, int k)
        {
            var clone = wbm.Clone();

            Octree o = new Octree(clone);

            return o.GenerateBitmapFromOctree(k);
        }
 private void RenderWorldFinish(WriteableBitmap img)
 {
     if (img != null) WorldImage.Image = img.Clone();
     RaisePropertyChanged("WorldZoomedHeight");
     RaisePropertyChanged("WorldZoomedWidth");
     CommandManager.InvalidateRequerySuggested();
 }
        /// <summary>
        /// Returns a copy of the given bitmap eroded by 1 pixel
        /// </summary>
        /// <param name="bitmap">The bitmap to be eroded</param>
        /// <returns>The eroded bitmap</returns>
        public static WriteableBitmap GetErodedBitmap( WriteableBitmap bitmap )
        {
            int stride = ( bitmap.PixelWidth * bitmap.Format.BitsPerPixel + 7 ) / 8;

             byte[] pixelArray = new byte[bitmap.PixelHeight * stride];
             byte[] newPixelArray = new byte[bitmap.PixelHeight * stride];
             bitmap.CopyPixels( pixelArray, stride, 0 );

             for ( int column = 0; column < bitmap.PixelWidth; column++ )
             {
            for ( int row = 0; row < bitmap.PixelHeight; row++ )
            {
               int index = row * stride + 4 * column;

               // If we are on the boundry just put white in the new image
               if ( column == 0 || row == 0 || column == bitmap.PixelWidth - 1 || row == bitmap.PixelHeight - 1 )
               {
                  newPixelArray[index] = 255;
                  newPixelArray[index + 1] = 255;
                  newPixelArray[index + 2] = 255;
                  continue;
               }

               var currentPixel = Pixel.GetPixel( pixelArray, index );
               var topPixel = Pixel.GetPixel( pixelArray, index - stride );
               var leftPixel = Pixel.GetPixel( pixelArray, index - 4 );
               var rightPixel = Pixel.GetPixel( pixelArray, index + 4 );
               var bottomPixel = Pixel.GetPixel( pixelArray, index + stride );

               // If the current pixel and all of its neighbored pixels are colored (not white), the new pixel is black
               if ( topPixel.Color != Colors.White &&
                    leftPixel.Color != Colors.White &&
                    rightPixel.Color != Colors.White &&
                    bottomPixel.Color != Colors.White &&
                    currentPixel.Color != Colors.White )
               {
                  newPixelArray[index] = 0;
                  newPixelArray[index + 1] = 0;
                  newPixelArray[index + 2] = 0;
               }
               else
               {
                  // Otherwise we make the pixel white
                  newPixelArray[index] = 255;
                  newPixelArray[index + 1] = 255;
                  newPixelArray[index + 2] = 255;
               }
            }
             }
             var erodedBitmap = bitmap.Clone();
             erodedBitmap.WritePixels( new Int32Rect( 0, 0, bitmap.PixelWidth, bitmap.PixelHeight ), newPixelArray, stride, 0 );

             return erodedBitmap;
        }
        /// <summary>
        /// Calculates the differences between the two bitmap and returns a bitmap where those differences
        /// are black and the rest of the bitmap is white
        /// </summary>
        /// <param name="firstBitmap">The first bitmap to compare</param>
        /// <param name="secondBitmap">The second bitmap to compare</param>
        /// <returns>A bitmap where all the black pixels represent where the two bitmaps were different
        /// and the white pixels represent where they were the same</returns>
        private static WriteableBitmap ExclusiveOrBitmaps( WriteableBitmap firstBitmap, WriteableBitmap secondBitmap )
        {
            int stride = ( firstBitmap.PixelWidth * firstBitmap.Format.BitsPerPixel + 7 ) / 8;

             byte[] firstPixelArray = new byte[firstBitmap.PixelHeight * stride];
             byte[] secondPixelArray = new byte[secondBitmap.PixelHeight * stride];
             byte[] ordPixelArray = new byte[secondBitmap.PixelHeight * stride];
             firstBitmap.CopyPixels( firstPixelArray, stride, 0 );
             secondBitmap.CopyPixels( secondPixelArray, stride, 0 );

             for ( int i = 0; i < firstBitmap.PixelWidth; i++ )
             {
            for ( int j = 0; j < firstBitmap.PixelHeight; j++ )
            {
               int index = j * stride + 4 * i;

               var firstBitmapPixel = Pixel.GetPixel( firstPixelArray, index );
               var secondBitmapPixel = Pixel.GetPixel( secondPixelArray, index );

               // If the pixels are the same, they are white in the exclusive or bitmap
               if ( firstBitmapPixel.Color == secondBitmapPixel.Color )
               {
                  ordPixelArray[index] = 255;
                  ordPixelArray[index + 1] = 255;
                  ordPixelArray[index + 2] = 255;
               }
               else
               {
                  // Otherwise black
                  ordPixelArray[index] = 0;
                  ordPixelArray[index + 1] = 0;
                  ordPixelArray[index + 2] = 0;
               }
            }
             }
             var ordBitmap = firstBitmap.Clone();
             ordBitmap.WritePixels( new Int32Rect( 0, 0, firstBitmap.PixelWidth, firstBitmap.PixelHeight ), ordPixelArray, stride, 0 );

             return ordBitmap;
        }
Example #16
0
        /// <summary>
        /// Rotates the bitmap in 90?steps clockwise and returns a new rotated WriteableBitmap.
        /// </summary>
        /// <param name="bmp">The WriteableBitmap.</param>
        /// <param name="angle">The angle in degrees the bitmap should be rotated in 90?steps clockwise.</param>
        /// <returns>A new WriteableBitmap that is a rotated version of the input.</returns>
        public static WriteableBitmap Rotate(this WriteableBitmap bmp, int angle)
        {
            using (var context = bmp.GetBitmapContext(ReadWriteMode.ReadOnly))
            {
                // Use refs for faster access (really important!) speeds up a lot!
                var             w      = context.Width;
                var             h      = context.Height;
                var             p      = context.Pixels;
                var             i      = 0;
                WriteableBitmap result = null;
                angle %= 360;

                if (angle > 0 && angle <= 90)
                {
                    result = BitmapFactory.New(h, w);
                    using (var destContext = result.GetBitmapContext())
                    {
                        var rp = destContext.Pixels;
                        for (var x = 0; x < w; x++)
                        {
                            for (var y = h - 1; y >= 0; y--)
                            {
                                var srcInd = y * w + x;
                                rp[i] = p[srcInd];
                                i++;
                            }
                        }
                    }
                }
                else if (angle > 90 && angle <= 180)
                {
                    result = BitmapFactory.New(w, h);
                    using (var destContext = result.GetBitmapContext())
                    {
                        var rp = destContext.Pixels;
                        for (var y = h - 1; y >= 0; y--)
                        {
                            for (var x = w - 1; x >= 0; x--)
                            {
                                var srcInd = y * w + x;
                                rp[i] = p[srcInd];
                                i++;
                            }
                        }
                    }
                }
                else if (angle > 180 && angle <= 270)
                {
                    result = BitmapFactory.New(h, w);
                    using (var destContext = result.GetBitmapContext())
                    {
                        var rp = destContext.Pixels;
                        for (var x = w - 1; x >= 0; x--)
                        {
                            for (var y = 0; y < h; y++)
                            {
                                var srcInd = y * w + x;
                                rp[i] = p[srcInd];
                                i++;
                            }
                        }
                    }
                }
                else
                {
                    result = bmp.Clone();
                }
                return(result);
            }
        }
Example #17
0
 public WriteableBitmap Apply(WriteableBitmap input)
 {
     int width = this.Watermark.PixelWidth;
     int height = this.Watermark.PixelHeight;
     float num3 = ((float)width) / ((float)height);
     if (num3 > 1f)
     {
         width = (int)(input.PixelWidth * this.RelativeSize);
         height = (int)(((float)width) / num3);
     }
     else
     {
         height = (int)(input.PixelHeight * this.RelativeSize);
         width = (int)(height * num3);
     }
     WriteableBitmap source = this.Watermark.Resize(width, height, WriteableBitmapExtensions.Interpolation.Bilinear);
     WriteableBitmap bmp = input.Clone();
     Rect destRect = new Rect((double)(input.PixelWidth - width), (double)(input.PixelHeight - height), (double)width, (double)height);
     bmp.Blit(destRect, source, new Rect(0.0, 0.0, (double)width, (double)height));
     return bmp;
 }