Esempio n. 1
0
        public static unsafe void RectShouldHaveColor(this WriteableBitmap b, int x0, int y0, int x1, int y1, Color color)
        {
            var expectedColor     = WriteableBitmapExtensions.ConvertColor(color);
            var expectedColorName = GetColorName(color);

            using (var c = b.GetBitmapContext(ReadWriteMode.ReadOnly))
            {
                x1 = Clamp(x1, 0, c.Width - 1);
                y1 = Clamp(y1, 0, c.Height - 1);
                x0 = Clamp(x0, 0, x1);
                y0 = Clamp(y0, 0, y1);

                for (var y = y0; y < y1; y++)
                {
                    for (var x = x0; x < x1; x++)
                    {
                        var actualColor = c.Pixels[y * c.Width + x];
                        if (actualColor != expectedColor)
                        {
                            var actualColorName = GetColorName(b.GetPixel(x, y));
                            Assert.Fail($"Pixel at ({x},{y}) should be '{expectedColorName}' but is '{actualColorName}'");
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        public Color GetPixel(WriteableBitmap wb)
        {
            int pixel; //ARGB variable with 32 int bytes where
                       //sets of 8 bytes are: Alpha, Red, Green, Blue
            byte r = 0;
            byte g = 0;
            byte b = 0;
            int  i = 0;
            int  j = 0;

            //Skip every alternate pixel making the program 4 times faster
            for (i = 0; i < wb.PixelWidth; i = i + 2)
            {
                for (j = 0; j < wb.PixelHeight; j = j + 2)
                {
                    pixel = WriteableBitmapExtensions.GetPixeli(wb, i, j); //the ARGB integer(pixel) has the colors of pixel (i,j)
                    r     = (byte)(r + (255 & (pixel >> 16)));             //add up reds
                    g     = (byte)(g + (255 & (pixel >> 8)));              //add up greens
                    b     = (byte)(b + (255 & (pixel)));                   //add up blues
                }
            }
            r = (byte)(r / (wb.PixelWidth / 2 * wb.PixelHeight / 2));
            g = (byte)(g / (wb.PixelWidth / 2 * wb.PixelHeight / 2));
            b = (byte)(b / (wb.PixelWidth / 2 * wb.PixelHeight / 2));
            return(Color.FromArgb(100, r, g, b));
        }
Esempio n. 3
0
            public static async Task FillBitmap(WriteableBitmap bmp, Func <float, float, Color> fillPixel)
            {
#if WINDOWS_PHONE
#else
                var stream = bmp.PixelBuffer.AsStream();
#endif
                int width  = bmp.PixelWidth;
                int height = bmp.PixelHeight;
                await Task.Run(() =>
                {
                    for (int y = 0; y < width; y++)
                    {
                        for (int x = 0; x < height; x++)
                        {
                            var color = fillPixel((float)x / width, (float)y / height);

#if WINDOWS_PHONE
                            bmp.Pixels[x + y *width] = WriteableBitmapExtensions.ConvertColor(color);
#else
                            WriteBGRA(stream, color);
#endif
                        }
                    }
                });

#if !WINDOWS_PHONE
                stream.Dispose();
#endif
                bmp.Invalidate();
            }
Esempio n. 4
0
        private void AddPolygonRing(WriteableBitmap context, SqlGeometry polygon, Func <Point, Point> transform, int border, int fill)
        {
            int numberOfPoints = polygon.STNumPoints().Value;

            int[] points = new int[2 * numberOfPoints];

            //STPointN(index): index is between 1 and number of points
            for (int i = 0; i < numberOfPoints; i++)
            {
                var point = transform(polygon.STPointN(i + 1).AsWpfPoint());

                points[2 * i] = (int)point.X;

                points[2 * i + 1] = (int)point.Y;
            }

            //if (border.HasValue)
            //{
            WriteableBitmapExtensions.DrawPolylineAa(context, points, border);
            //}

            //if (fill.HasValue)
            //{
            WriteableBitmapExtensions.FillPolygon(context, points, fill);
            //}
        }
Esempio n. 5
0
        //int intBorderColor;

        //int intFillColor;

        public WriteableBitmap ParseSqlGeometry(List <SqlGeometry> geometries, Func <Point, Point> transform, int width, int height, Color border, Color fill, ImageSource pointSymbol = null, sb.Primitives.Geometry <sb.Primitives.Point> symbol = null)
        {
            //int? intBorderColor = border.HasValue ? WriteableBitmapExtensions.ConvertColor(border.Value) : (int?)null;

            //int? intFillColor = fill.HasValue ? WriteableBitmapExtensions.ConvertColor(fill.Value) : (int?)null;
            int intBorderColor = WriteableBitmapExtensions.ConvertColor(border);

            int intFillColor = WriteableBitmapExtensions.ConvertColor(fill);

            WriteableBitmap result = new WriteableBitmap(width, height, 96, 96, PixelFormats.Pbgra32, null);

            if (geometries != null)
            {
                using (result.GetBitmapContext())
                {
                    foreach (SqlGeometry item in geometries)
                    {
                        AddGeometry(result, item, transform, intBorderColor, intFillColor, pointSymbol, symbol);
                    }
                }
            }

            //result.Freeze();

            return(result);
        }
Esempio n. 6
0
        public EdgeProgressBar()
        {
            this.Loaded += (s, e) =>
            {
                if (bScaleBackToPixels)
                {
                    Matrix         m            = PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice;
                    ScaleTransform dpiTransform = new ScaleTransform(1 / m.M11, 1 / m.M22);
                    if (dpiTransform.CanFreeze)
                    {
                        dpiTransform.Freeze();
                    }
                    this.LayoutTransform = dpiTransform;
                }

                ProgressWidth = (int)this.ActualWidth - 20;
                bm            = BitmapFactory.New(ProgressWidth, 8);
                image.Source  = bm;
                image.Effect  = null;

                RenderGeometry();
            };

            InitializeComponent();
            backRectFill   = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#B4B4B4"));
            backRectStroke = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#ABABAB"));
            backEmpty      = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#FFFFFF"));
            r0             = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#4687E8"));
            l1             = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#79C0F9"));
            l2             = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#6DB3F7"));
            l3             = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#63A8F7"));
            l4             = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#589CF5"));
            lm1            = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#59A3F6"));
            lm2            = WriteableBitmapExtensions.ConvertColor((Color)ColorConverter.ConvertFromString("#5095F6"));
        }
        private async Task <WriteableBitmap> GetTestImageAsync(string name)
        {
            // クラスライブラリ内の画像をリソースを読み出す
            var imageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/" + name));

            // StorageFileからWriteableBitmapを生成する
            return(await WriteableBitmapExtensions.FromStreamAsync(await imageFile.OpenReadAsync()));
        }
Esempio n. 8
0
        public static WriteableBitmap CreateImage(EntityModel entityModel)
        {
            WriteableBitmap bankBitmap = BitmapFactory.New(64, 64);

            using (bankBitmap.GetBitmapContext())
            {
                foreach (CharacterTile tile in entityModel.Frame.Tiles)
                {
                    if (string.IsNullOrEmpty(tile.BankID) || string.IsNullOrEmpty(tile.BankTileID))
                    {
                        continue;
                    }

                    BankModel ptModel = ProjectFiles.GetModel <BankModel>(tile.BankID);
                    if (ptModel == null)
                    {
                        continue;
                    }

                    PTTileModel bankModel = ptModel.GetTileModel(tile.BankTileID);

                    if (string.IsNullOrEmpty(bankModel.GUID) && string.IsNullOrEmpty(bankModel.TileSetID))
                    {
                        continue;
                    }

                    WriteableBitmap sourceBitmap = CreateImageUtil.GetCacheBitmap(bankModel.TileSetID);

                    if (sourceBitmap == null)
                    {
                        continue;
                    }

                    using (sourceBitmap.GetBitmapContext())
                    {
                        WriteableBitmap cropped = sourceBitmap.Crop((int)bankModel.Point.X, (int)bankModel.Point.Y, 8, 8);

                        if (tile.FlipX)
                        {
                            cropped = WriteableBitmapExtensions.Flip(cropped, WriteableBitmapExtensions.FlipMode.Vertical);
                        }

                        if (tile.FlipY)
                        {
                            cropped = WriteableBitmapExtensions.Flip(cropped, WriteableBitmapExtensions.FlipMode.Horizontal);
                        }

                        int destX = (int)Math.Floor(tile.Point.X / 8) * 8;
                        int destY = (int)Math.Floor(tile.Point.Y / 8) * 8;

                        Util.CopyBitmapImageToWriteableBitmap(ref bankBitmap, destX, destY, cropped);
                    }
                }
            }

            return(bankBitmap);
        }
        internal static async Task <WriteableBitmap> FromStreamtoWritableBitmap(InMemoryRandomAccessStream stream)
        {
            WriteableBitmap   writableBitmap = new WriteableBitmap(1, 1);
            BitmapPixelFormat format         = BitmapPixelFormat.Unknown;

            writableBitmap = await WriteableBitmapExtensions.FromStream(writableBitmap, stream, format);

            return(writableBitmap);
        }
Esempio n. 10
0
        /// <summary>
        /// Saves the cropped image to a stream with the specified format.
        /// </summary>
        /// <param name="stream">The target stream.</param>
        /// <param name="bitmapFileFormat">the specified format.</param>
        /// <returns>Task</returns>
        public async Task SaveAsync(IRandomAccessStream stream, BitmapFileFormat bitmapFileFormat)
        {
            if (SourceImage == null)
            {
                return;
            }
            var bitmapEncoder = await BitmapEncoder.CreateAsync(WriteableBitmapExtensions.GetEncoderId(bitmapFileFormat), stream);

            await WriteableBitmapExtensions.CropImageAsync(SourceImage, _currentCroppedRect, bitmapEncoder);
        }
Esempio n. 11
0
        public static WriteableBitmap WriteableBitmapBlur(this WriteableBitmap writeableBitmapOld, KernelType kernelType = KernelType.KernelGaussianBlur5x5)
        {
            //var cloneWriteableBitmap = WriteableBitmapExpansions.CopyWriteableBitmap(writeableBitmapOld);
            var kernelValue = 5;

            kernelValue = ConverKernelValue(kernelType);
            WriteableBitmap writeableBitmapBlur = WriteableBitmapExtensions.Convolute(writeableBitmapOld, ConverKernelType(kernelType), kernelValue, kernelValue);

            writeableBitmapBlur.Invalidate();
            return(writeableBitmapBlur);
        }
        private async void btnSavePng_Click(object sender, RoutedEventArgs e)
        {
            var bitmap = imageDst.Source as WriteableBitmap;

            if (bitmap != null)
            {
                await bitmap.SaveAsync(ImageDirectories.InApplicationLocal, ImageFormat.Png, "effect_sample");

                var bmp = await WriteableBitmapExtensions.LoadAsync(ImageDirectories.InApplicationLocal, ImageFormat.Png, "effect_sample");
            }
        }
Esempio n. 13
0
        private int AddColors(int col1, int col2)
        {
            var c1 = GetColor(col1);
            var c2 = GetColor(col2);

            var result = Color.FromArgb((byte)(Math.Min(255, c1.A + c2.A)),
                                        (byte)(Math.Min(255, c1.R + c2.R)),
                                        (byte)(Math.Min(255, c1.G + c2.G)),
                                        (byte)(Math.Min(255, c1.B + c2.B)));

            return(WriteableBitmapExtensions.ConvertColor(result));
        }
        public void DrawLines(double[] vertices, DicomColor color)
        {
            var actualColor = WriteableBitmapExtensions.ConvertColor(DicomColorConverter.FromDicomColor(color));

            using (var context = _bitmap.GetBitmapContext())
            {
                for (int i = 0; i < vertices.Length; i += 4)
                {
                    WriteableBitmapExtensions.DrawLine(context, context.Width, context.Height, transxi(vertices[i]), transyi(vertices[i + 1]), transxi(vertices[i + 2]), transyi(vertices[i + 3]), actualColor);
                }
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Resize the WriteableBitmap
        /// </summary>
        /// <param name="srcBitmap"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public void ResizeWriteableBitmap(ref WriteableBitmap srcBitmap, ref WriteableBitmap destBitmap, int width, int height)
        {
            var resizedBitmap = WriteableBitmapExtensions.Resize(srcBitmap, width, height, WriteableBitmapExtensions.Interpolation.Bilinear);

            Rect rec = new Rect(0, 0, width, height);

            using (resizedBitmap.GetBitmapContext()) {
                using (resizedBitmap.GetBitmapContext()) {
                    destBitmap.Blit(rec, resizedBitmap, rec, WriteableBitmapExtensions.BlendMode.None);
                }
            }
        }
Esempio n. 16
0
        public async Task SaveCroppedBitmapAsync(StorageFile imageFile, Guid encoderId)
        {
            if (SourceImage == null)
            {
                return;
            }
            using (var fileStream = await imageFile.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.None))
            {
                var bitmapEncoder = await BitmapEncoder.CreateAsync(encoderId, fileStream);

                await WriteableBitmapExtensions.CropImageAsync(SourceImage, _currentCroppedRect, bitmapEncoder);
            }
        }
Esempio n. 17
0
 // десериализация из бинарника
 public Layer(SerializationInfo sInfo, StreamingContext contextArg)
 {
     BitMap = new WriteableBitmap(new BitmapImage(new Uri("UI/Empty.png", UriKind.Relative)));
     WriteableBitmapExtensions.FromByteArray(BitMap, (byte[])sInfo.GetValue("BitMap", typeof(byte[])));
     Visibility = (string)sInfo.GetValue("Visibility", typeof(string));
     ToolTip    = (string)sInfo.GetValue("ToolTip", typeof(string));
     X          = (double)sInfo.GetValue("X", typeof(double));
     Y          = (double)sInfo.GetValue("Y", typeof(double));
     Opacity    = 1;// (double)sInfo.GetValue("Opacity", typeof(double));
     Height     = (double)sInfo.GetValue("Height", typeof(double));
     Width      = (double)sInfo.GetValue("Width", typeof(double));
     BlurRadius = (double)sInfo.GetValue("BlurRadius", typeof(double));
 }
Esempio n. 18
0
 private void PreviewImage_OnLoaded(object sender, RoutedEventArgs e)
 {
     _bitmap = BitmapFactory.New(500, 500);
     for (var y = 0; y < 500; ++y)
     {
         for (var x1 = 0; x1 < 250; ++x1)
         {
             _bitmap.SetPixel(x1, y, WriteableBitmapExtensions.ConvertColor(Colors.DodgerBlue));
             _bitmap.SetPixel(x1 + 250, y, WriteableBitmapExtensions.ConvertColor(Colors.SeaGreen));
         }
     }
     _bitmap.FillEllipseCentered(225, 225, 50, 50, WriteableBitmapExtensions.ConvertColor(0.5, Colors.Red), true);
     //_bitmap.FillRectangle(200, 200, 250, 250, WriteableBitmapExtensions.ConvertColor(0.5, Colors.Red), true);
     PreviewImage.Source = _bitmap;
 }
Esempio n. 19
0
        public static WriteableBitmap WriteableBitmapBlur(this WriteableBitmap writeableBitmapOld, int[,] GaussianBlur, int kernelFactorSum, int kernelOffsetSum)
        {
            //var cloneWriteableBitmap = WriteableBitmapExpansions.CopyWriteableBitmap(writeableBitmapOld);
            WriteableBitmap writeableBitmapBlur = null;

            if (GaussianBlur == null)
            {
                writeableBitmapBlur = WriteableBitmapExtensions.Convolute(writeableBitmapOld, WriteableBitmapExtensions.KernelGaussianBlur5x5, 5, 5);
            }
            else
            {
                writeableBitmapBlur = WriteableBitmapExtensions.Convolute(writeableBitmapOld, GaussianBlur, kernelFactorSum, kernelOffsetSum);
            }
            writeableBitmapBlur.Invalidate();
            return(writeableBitmapBlur);
        }
Esempio n. 20
0
        public static async Task <WriteableBitmap> GenerateBusMapIcon(StopDirection direction)
        {
            var bus = await GenerateBusMapIcon();

            if (direction == StopDirection.Unspecified)
            {
                return(bus);
            }
            var arrow = await WriteableBitmapExtensions.FromContent(null, new Uri("ms-appx:///Assets/Icons/ArrowBase.png"));

            int angle = (int)direction * 45 - 45;

            arrow = arrow.RotateFree(angle);
            bus.Blit(new Windows.Foundation.Rect(43, 36, 170, 170), arrow, new Windows.Foundation.Rect(0, 0, 170, 170), WriteableBitmapExtensions.BlendMode.Alpha);
            return(bus);
        }
Esempio n. 21
0
        /// <summary>
        /// Draws random shapes.
        /// </summary>
        private unsafe void DrawShapes()
        {
            // Wrap updates in a GetContext call, to prevent invalidation and nested locking/unlocking during this block
            using (var bitmapContext = writeableBmp.GetBitmapContext())
            {
                // Init some size vars
                int w  = this.writeableBmp.PixelWidth - 2;
                int h  = this.writeableBmp.PixelHeight - 2;
                int wh = w >> 1;
                int hh = h >> 1;

                // Clear
                writeableBmp.Clear();

                // Draw Shapes and use refs for faster access which speeds up a lot.
                int wbmp   = writeableBmp.PixelWidth;
                int hbmp   = writeableBmp.PixelHeight;
                var pixels = bitmapContext.Pixels;
                for (int i = 0; i < shapeCount / 6; i++)
                {
                    // Standard shapes
                    WriteableBitmapExtensions.DrawLine(bitmapContext, wbmp, hbmp, rand.Next(w), rand.Next(h), rand.Next(w),
                                                       rand.Next(h), GetRandomColor());
                    writeableBmp.DrawTriangle(rand.Next(w), rand.Next(h), rand.Next(w), rand.Next(h), rand.Next(w),
                                              rand.Next(h), GetRandomColor());
                    writeableBmp.DrawQuad(rand.Next(w), rand.Next(h), rand.Next(w), rand.Next(h), rand.Next(w),
                                          rand.Next(h), rand.Next(w), rand.Next(h), GetRandomColor());
                    writeableBmp.DrawRectangle(rand.Next(wh), rand.Next(hh), rand.Next(wh, w), rand.Next(hh, h),
                                               GetRandomColor());
                    writeableBmp.DrawEllipse(rand.Next(wh), rand.Next(hh), rand.Next(wh, w), rand.Next(hh, h),
                                             GetRandomColor());

                    // Random polyline
                    int[] p = new int[rand.Next(5, 10) * 2];
                    for (int j = 0; j < p.Length; j += 2)
                    {
                        p[j]     = rand.Next(w);
                        p[j + 1] = rand.Next(h);
                    }
                    writeableBmp.DrawPolyline(p, GetRandomColor());
                }

                // Invalidates on end of using block
            }
        }
        public async Task <WriteableBitmap> RecognizePlateAsync(IRandomAccessStream imageStream)
        {
            var writeableBitmap = await WriteableBitmapExtensions.FromStream(null, imageStream);

            await SaveImageAsync(writeableBitmap);

            var processedImage         = GetEdgedImage(writeableBitmap.Clone());
            var possiblePlatePositions = await GetPlateRectanglesAsync(processedImage);

            var plate = FindPlate(possiblePlatePositions, writeableBitmap);

            if (plate == null)
            {
                return(null);
            }

            await SaveImageAsync(plate);

            return(plate);
        }
Esempio n. 23
0
        public static async Task <WriteableBitmap> GetBusSprite(string text, double angle)
        {
            Rect[]          nonColoredRegions = new Rect[] { new Rect(0, 1, 1, 41), new Rect(4, 2, 3, 39), new Rect(12, 8, 73, 27), new Rect(95, 11, 11, 22), new Rect(125, 0, 0, 43) };
            WriteableBitmap result;

            if (VehicleBaseImage == null)
            {
                result = await WriteableBitmapExtensions.FromContent(null, new Uri("ms-appx:///Assets/Icons/VehicleBase.png"));

                Color accentColor          = ((Color)App.Current.Resources["SystemColorControlAccentColor"]);
                Func <Color, Color> darken = clr => Color.FromArgb(clr.A, (byte)(clr.R / 2), (byte)(clr.G / 2), (byte)(clr.B / 2));
                int innerWidth             = 126;
                int innerHeight            = 44;
                result.ForEach((x, y, oldCol) =>
                {
                    Color newColor = oldCol;
                    double relX    = x - (result.PixelWidth - innerWidth) / 2;
                    double relY    = y - (result.PixelHeight - innerHeight) / 2;
                    if (!nonColoredRegions.Any(rect => rect.Contains(new Point(relX, relY))) && relX >= 0 && relY >= 0 && relX < innerWidth && relY < innerHeight)
                    {
                        newColor = SetByteBrightness(darken(accentColor), newColor.R);
                        //newColor = Color.FromArgb(newColor.A, (byte)(accentColor.R + ), (byte)((double)newColor.G * (double)accentColor.G / 256.0), (byte)((double)newColor.B * (double)accentColor.B / 256.0));
                    }
                    return(newColor);
                });
                VehicleBaseImage = result;
            }
            else
            {
                result = VehicleBaseImage;
            }
            result = result.RotateFree(angle);
            //result.ForEach((x, y, oldCol) =>
            //{
            //    Color newColor = oldCol;
            //    if (newColor.A == 0)
            //        newColor = Colors.Black;
            //    return newColor;
            //});
            return(result);
        }
Esempio n. 24
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 = WriteableBitmapExtensions.Clone(bmp);
                }
                return(result);
            }
        }
        public System.Windows.Media.Imaging.WriteableBitmap renderTileImg(menu menuEntry, bool rendertitle = false)
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                string i = (string)menuEntry.name;
                int    x = 0;
                int    y = 0;
                int    s = 0;
                int    z = 0;


                if (menuEntry.apps.Count < 5)
                {
                    s = 100;
                }
                else
                {
                    s = 67;
                }


                System.Windows.Media.Imaging.WriteableBitmap wb = new System.Windows.Media.Imaging.WriteableBitmap(200, 200);

                System.Windows.Media.Imaging.WriteableBitmap wb_back = new System.Windows.Media.Imaging.WriteableBitmap(200, 200);

                if (rendertitle)
                {
                    Rectangle rect = new Rectangle();
                    rect.Width  = 200;
                    rect.Height = 200;

                    Color clr = (Color)Application.Current.Resources["PhoneAccentColor"];
                    clr.A = 160;

                    rect.Fill = new SolidColorBrush(clr);

                    wb.Render(rect, null);
                    wb_back.Render(rect, null);
                }
                else
                {
                    Rectangle rect = new Rectangle();
                    rect.Width  = 200;
                    rect.Height = 200;

                    rect.Fill = new SolidColorBrush(Color.FromArgb(160, 0, 0, 0));

                    wb.Render(rect, null);
                    wb_back.Render(rect, null);
                }

                foreach (appentry app in menuEntry.apps)
                {
                    Image img = new Image();
                    img.Stretch = Stretch.Uniform;
                    img.Width   = s;
                    img.Height  = s;
                    BitmapImage bmp = new BitmapImage();
                    using (var file = store.OpenFile(app.guid + ".jpg", FileMode.Open, FileAccess.Read))
                    {
                        bmp.SetSource(file);
                    }
                    img.Source = bmp;

                    if (z < 9)
                    {
                        wb.Render(img, new TranslateTransform {
                            X = x, Y = y
                        });
                    }
                    else
                    {
                        wb_back.Render(img, new TranslateTransform {
                            X = x, Y = y
                        });
                    }
                    x += s;
                    if (x > 200 - (s - 5))
                    {
                        x  = 0;
                        y += s;
                    }
                    if (z == 8)
                    {
                        x = 0;
                        y = 0;
                    }
                    z++;
                }

                /*if (rendertitle)
                 * {
                 *  TextBlock title = new TextBlock();
                 *  title.FontSize = 21;
                 *  title.Text = i;
                 *  title.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
                 *
                 *  wb.Render(title, new TranslateTransform { X = 10, Y = 170 });
                 * }*/
                wb.Invalidate();

                System.Diagnostics.Debug.WriteLine(i);
                if (!rendertitle)
                {
                    var filename = "/Shared/ShellContent/tile_" + i + ".jpg";
                    using (var st = new IsolatedStorageFileStream(filename, FileMode.Create, FileAccess.Write, store))
                    {
                        WriteableBitmapExtensions.WritePNG(wb, st);
                    }
                    if (menuEntry.apps.Count > 9)
                    {
                        wb_back.Invalidate();
                        filename = "/Shared/ShellContent/tile_" + i + "_back.jpg";
                        using (var st = new IsolatedStorageFileStream(filename, FileMode.Create, FileAccess.Write, store))
                        {
                            WriteableBitmapExtensions.WritePNG(wb_back, st);
                        }
                    }
                }
                return(wb);
            }
        }
Esempio n. 26
0
        public unsafe void MyDrawLine(int x1, int y1, int x2, int y2, Color col, bool addColors)
        {
            int color       = WriteableBitmapExtensions.ConvertColor(col);
            int pixelWidth  = Width;
            int pixelHeight = Height;
            // Get clip coordinates
            int clipX1 = 0;
            int clipX2 = pixelWidth;
            int clipY1 = 0;
            int clipY2 = pixelHeight;

            // Perform cohen-sutherland clipping if either point is out of the viewport
            //if (!MyCohenSutherlandLineClip(new Rect(clipX1, clipY1, clipX2 - clipX1, clipY2 - clipY1), ref x1, ref y1, ref x2, ref y2)) return;

            // Distance start and end point
            int dx = x2 - x1;
            int dy = y2 - y1;

            const int PRECISION_SHIFT = 8;

            // Determine slope (absolute value)
            int lenX, lenY;

            if (dy >= 0)
            {
                lenY = dy;
            }
            else
            {
                lenY = -dy;
            }

            if (dx >= 0)
            {
                lenX = dx;
            }
            else
            {
                lenX = -dx;
            }

            if (lenX > lenY)
            { // x increases by +/- 1
                if (dx < 0)
                {
                    int t = x1;
                    x1 = x2;
                    x2 = t;
                    t  = y1;
                    y1 = y2;
                    y2 = t;
                }

                // Init steps and start
                int incy = (dy << PRECISION_SHIFT) / dx;

                int y1s = y1 << PRECISION_SHIFT;
                int y2s = y2 << PRECISION_SHIFT;
                int hs  = pixelHeight << PRECISION_SHIFT;

                if (y1 < y2)
                {
                    if (y1 >= clipY2 || y2 < clipY1)
                    {
                        return;
                    }
                    if (y1s < 0)
                    {
                        if (incy == 0)
                        {
                            return;
                        }
                        int oldy1s = y1s;
                        // Find lowest y1s that is greater or equal than 0.
                        y1s = incy - 1 + ((y1s + 1) % incy);
                        x1 += (y1s - oldy1s) / incy;
                    }
                    if (y2s >= hs)
                    {
                        if (incy != 0)
                        {
                            // Find highest y2s that is less or equal than ws - 1.
                            // y2s = y1s + n * incy. Find n.
                            y2s = hs - 1 - (hs - 1 - y1s) % incy;
                            x2  = x1 + (y2s - y1s) / incy;
                        }
                    }
                }
                else
                {
                    if (y2 >= clipY2 || y1 < clipY1)
                    {
                        return;
                    }
                    if (y1s >= hs)
                    {
                        if (incy == 0)
                        {
                            return;
                        }
                        int oldy1s = y1s;
                        // Find highest y1s that is less or equal than ws - 1.
                        // y1s = oldy1s + n * incy. Find n.
                        y1s = hs - 1 + (incy - (hs - 1 - oldy1s) % incy);
                        x1 += (y1s - oldy1s) / incy;
                    }
                    if (y2s < 0)
                    {
                        if (incy != 0)
                        {
                            // Find lowest y2s that is greater or equal than 0.
                            // y2s = y1s + n * incy. Find n.
                            y2s = y1s % incy;
                            x2  = x1 + (y2s - y1s) / incy;
                        }
                    }
                }

                if (x1 < 0)
                {
                    y1s -= incy * x1;
                    x1   = 0;
                }
                if (x2 >= pixelWidth)
                {
                    x2 = pixelWidth - 1;
                }

                int ys = y1s;

                // Walk the line!
                int y         = ys >> PRECISION_SHIFT;
                int previousY = y;
                int index     = x1 + y * pixelWidth;
                int k         = incy < 0 ? 1 - pixelWidth : 1 + pixelWidth;
                for (int x = x1; x <= x2; ++x)
                {
                    SetPixel(index, addColors ? AddColors(GetPixel(index), color) : color);
                    ys += incy;
                    y   = ys >> PRECISION_SHIFT;
                    if (y != previousY)
                    {
                        previousY = y;
                        index    += k;
                    }
                    else
                    {
                        ++index;
                    }
                }
            }
            else
            {
                // Prevent division by zero
                if (lenY == 0)
                {
                    return;
                }
                if (dy < 0)
                {
                    int t = x1;
                    x1 = x2;
                    x2 = t;
                    t  = y1;
                    y1 = y2;
                    y2 = t;
                }

                // Init steps and start
                int x1s = x1 << PRECISION_SHIFT;
                int x2s = x2 << PRECISION_SHIFT;
                int ws  = pixelWidth << PRECISION_SHIFT;

                int incx = (dx << PRECISION_SHIFT) / dy;

                if (x1 < x2)
                {
                    if (x1 >= clipX2 || x2 < clipX1)
                    {
                        return;
                    }
                    if (x1s < 0)
                    {
                        if (incx == 0)
                        {
                            return;
                        }
                        int oldx1s = x1s;
                        // Find lowest x1s that is greater or equal than 0.
                        x1s = incx - 1 + ((x1s + 1) % incx);
                        y1 += (x1s - oldx1s) / incx;
                    }
                    if (x2s >= ws)
                    {
                        if (incx != 0)
                        {
                            // Find highest x2s that is less or equal than ws - 1.
                            // x2s = x1s + n * incx. Find n.
                            x2s = ws - 1 - (ws - 1 - x1s) % incx;
                            y2  = y1 + (x2s - x1s) / incx;
                        }
                    }
                }
                else
                {
                    if (x2 >= clipX2 || x1 < clipX1)
                    {
                        return;
                    }
                    if (x1s >= ws)
                    {
                        if (incx == 0)
                        {
                            return;
                        }
                        int oldx1s = x1s;
                        // Find highest x1s that is less or equal than ws - 1.
                        // x1s = oldx1s + n * incx. Find n.
                        x1s = ws - 1 + (incx - (ws - 1 - oldx1s) % incx);
                        y1 += (x1s - oldx1s) / incx;
                    }
                    if (x2s < 0)
                    {
                        if (incx != 0)
                        {
                            // Find lowest x2s that is greater or equal than 0.
                            // x2s = x1s + n * incx. Find n.
                            x2s = x1s % incx;
                            y2  = y1 + (x2s - x1s) / incx;
                        }
                    }
                }

                if (y1 < 0)
                {
                    x1s -= incx * y1;
                    y1   = 0;
                }
                if (y2 >= pixelHeight)
                {
                    y2 = pixelHeight - 1;
                }

                int index          = x1s;
                int indexBaseValue = y1 * pixelWidth;

                // Walk the line!
                var inc = (pixelWidth << PRECISION_SHIFT) + incx;
                for (int y = y1; y <= y2; ++y)
                {
                    var idx = indexBaseValue + (index >> PRECISION_SHIFT);
                    SetPixel(idx, addColors ? AddColors(GetPixel(idx), color) : color);
                    index += inc;
                }
            }
        }
Esempio n. 27
0
 public override async Task Load()
 {
     Bitmap = await WriteableBitmapExtensions.FromContent(null, ImageUri);
 }
Esempio n. 28
0
 public static async Task <WriteableBitmap> GenerateBusMapIcon()
 {
     return(await WriteableBitmapExtensions.FromContent(null, new Uri("ms-appx:///Assets/Icons/BusBase.png")));
 }
Esempio n. 29
0
        private void DrawWaveform()
        {
            float[] peaks           = ((UWavePart)Part).Peaks;
            int     x               = 0;
            double  width           = Part.DurTick * ScaleX;
            double  height          = _height;
            double  samplesPerPixel = peaks.Length / width;

            using (BitmapContext cxt = partBitmap.GetBitmapContext())
            {
                double monoChnlAmp   = (height - 4) / 2;
                double stereoChnlAmp = (height - 6) / 4;

                int channels = ((UWavePart)Part).Channels;
                partBitmap.Clear();
                float left, right, lmax, lmin, rmax, rmin;
                lmax = lmin = rmax = rmin = 0;
                double position = 0;

                int skippedPixels = (int)Math.Round(Math.Max(0, -this.X));
                if (skippedPixels > 0)
                {
                    skippedPixels -= 1; x -= 1;
                }                                                      // draw 1 pixel out of view
                else if (this.X > 0)
                {
                    x = (int)Math.Round(this.X);
                }
                position += skippedPixels * samplesPerPixel;

                for (int i = (int)(position / channels) * channels; i < peaks.Length; i += channels)
                {
                    left  = peaks[i];
                    right = peaks[i + 1];
                    lmax  = Math.Max(left, lmax);
                    lmin  = Math.Min(left, lmin);
                    if (channels > 1)
                    {
                        rmax = Math.Max(right, rmax);
                        rmin = Math.Min(right, rmin);
                    }
                    if (i > position)
                    {
                        if (channels > 1)
                        {
                            WriteableBitmapExtensions.DrawLine(
                                partBitmap,
                                x, (int)(stereoChnlAmp * (1 + lmin)) + 2,
                                x, (int)(stereoChnlAmp * (1 + lmax)) + 2,
                                Colors.White);
                            WriteableBitmapExtensions.DrawLine(
                                partBitmap,
                                x, (int)(stereoChnlAmp * (1 + rmin) + monoChnlAmp) + 3,
                                x, (int)(stereoChnlAmp * (1 + rmax) + monoChnlAmp) + 3,
                                Colors.White);
                        }
                        else
                        {
                            WriteableBitmapExtensions.DrawLine(
                                partBitmap,
                                x, (int)(monoChnlAmp * (1 + lmin)) + 2,
                                x, (int)(monoChnlAmp * (1 + lmax)) + 2,
                                Colors.White);
                        }
                        lmax      = lmin = rmax = rmin = 0;
                        position += samplesPerPixel;
                        x++;
                        if (x > CanvasWidth)
                        {
                            break;
                        }
                    }
                }
            }
        }
Esempio n. 30
0
        public static void UpdateTile()
        {
            var customTile = new LiveTile();

            customTile.UpdateTextBox(AppointmentsOnLiveTile);
            customTile.Measure(new Size(336, 336));
            customTile.Arrange(new Rect(0, 0, 336, 336));


            var customTileWide = new LiveTileWide();

            customTileWide.UpdateTextBox(AppointmentsOnLiveTile);
            customTileWide.Measure(new Size(691, 336));
            customTileWide.Arrange(new Rect(0, 0, 691, 336));

            var customTileSmall = new LiveTileSmall();

            customTileSmall.UpdateDate();
            customTileSmall.Measure(new Size(159, 159));
            customTileSmall.Arrange(new Rect(0, 0, 159, 159));


            var bmp = new WriteableBitmap(336, 336);

            bmp.Render(customTile, null);
            bmp.Invalidate();

            var bmp2 = new WriteableBitmap(691, 336);

            bmp2.Render(customTileWide, null);
            bmp2.Invalidate();

            var bmp3 = new WriteableBitmap(159, 159);

            bmp3.Render(customTileSmall, null);
            bmp3.Invalidate();

            const string filename          = "CustomTile";
            const string filenamefull      = "/Shared/ShellContent/CustomTile.png";
            const string filenameWide      = "CustomTileWide";
            const string filenameWidefull  = "/Shared/ShellContent/CustomTileWide.png";
            const string filenameSmall     = "CustomTileSmall";
            const string filenameSmallfull = "/Shared/ShellContent/CustomTileSmall.png";

            using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (
                    var imageStream = new IsolatedStorageFileStream("/Shared/ShellContent/" + filename + ".png",
                                                                    FileMode.OpenOrCreate, isf))
                {
                    WriteableBitmapExtensions.SavePng(bmp, imageStream);
                }

                using (
                    var imageStream = new IsolatedStorageFileStream("/Shared/ShellContent/" + filenameWide + ".png",
                                                                    FileMode.OpenOrCreate, isf))
                {
                    WriteableBitmapExtensions.SavePng(bmp2, imageStream);
                }

                using (
                    var imageStream = new IsolatedStorageFileStream("/Shared/ShellContent/" + filenameSmall + ".png",
                                                                    FileMode.OpenOrCreate, isf))
                {
                    WriteableBitmapExtensions.SavePng(bmp3, imageStream);
                }
            }



            var tileData = new FlipTileData
            {
                WideBackgroundImage  = new Uri("isostore:" + filenameWidefull, UriKind.Absolute),
                BackgroundImage      = new Uri("isostore:" + filenamefull, UriKind.Absolute),
                SmallBackgroundImage = new Uri("isostore:" + filenameSmallfull, UriKind.Absolute)
            };


            var currentTile = ShellTile.ActiveTiles.FirstOrDefault();

            if (currentTile != null)
            {
                currentTile.Update(tileData);
            }
        }