コード例 #1
0
        public unsafe static Image GetProgressBar(Upload upload, int width, int height)
        {
            if (upload == null)
            {
                throw new ArgumentNullException("upload");
            }
            if (width < 0)
            {
                throw new ArgumentOutOfRangeException("width");
            }
            if (height < 0)
            {
                throw new ArgumentOutOfRangeException("height");
            }

            Bitmap     bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            BitmapData bmd    = bitmap.LockBits(new Rectangle(new Point(0, 0), bitmap.Size), ImageLockMode.ReadOnly, bitmap.PixelFormat);
            byte *     pbmd;
            int        y;
            int        x;
            int        sentLeft      = (int)Math.Floor((double)Math.Max(upload.LastSentSector, 0) / (double)upload.Sectors * (double)bitmap.Width);
            int        requestedLeft = (int)Math.Floor((double)Math.Max(upload.LastRequestedSector, 0) / (double)upload.Sectors * (double)bitmap.Width);
            int        progressWidth = (int)Math.Ceiling((double)bitmap.Width / (double)upload.Sectors);

            bool[] progressBarMap = new bool[width];
            ScaleLinear(upload.SectorsMap, (int)upload.Sectors, progressBarMap);
            Color neither = Color.FromArgb(224, 224, 224);
            Color both;
            Color sending     = Color.FromArgb(0, 150, 0);
            Color nextSending = Color.FromArgb(255, 208, 0);

            if (bool.Parse(Settings.Instance["ProgressBarsHaveShadow"]))
            {
                both = Color.FromArgb(104, 104, 104);
                double[]      shadows           = ComputeShadows(bitmap.Height, int.Parse(Settings.Instance["ProgressBarsShadow"]));
                UnsafeColor[] neitherColors     = ComputeColors(neither, bitmap.Height, shadows);
                UnsafeColor[] bothColors        = ComputeColors(both, bitmap.Height, shadows);
                UnsafeColor[] sendingColors     = ComputeColors(sending, bitmap.Height, shadows);
                UnsafeColor[] nextSendingColors = ComputeColors(nextSending, bitmap.Height, shadows);
                UnsafeColor * c;
                fixed(bool *pSector = &progressBarMap[0])
                {
                    fixed(UnsafeColor *pBoth = &bothColors[0], pNeither = &neitherColors[0])
                    {
                        bool *p;

                        for (y = 0; y < bitmap.Height; y++)
                        {
                            p    = pSector;
                            pbmd = (byte *)bmd.Scan0 + y * bmd.Stride;
                            for (x = 0; x < bitmap.Width; x++)
                            {
                                c = *p++ ? &pBoth[y] : &pNeither[y];
                                *pbmd++ = c->B;
                                *pbmd++ = c->G;
                                *pbmd++ = c->R;
                            }
                        }
                    }
                }

                if (upload.IsActive)
                {
                    if (upload.LastSentSector > -1)
                        fixed(UnsafeColor *pSending = &sendingColors[0])
                        {
                            int n;

                            for (y = 0; y < bitmap.Height; y++)
                            {
                                pbmd = (byte *)bmd.Scan0 + y * bmd.Stride + sentLeft * 3;
                                for (n = 0, x = sentLeft; n < progressWidth && x < bitmap.Width; n++, x++)
                                {
                                    c = &pSending[y];
                                    *pbmd++ = c->B;
                                    *pbmd++ = c->G;
                                    *pbmd++ = c->R;
                                }
                            }
                        }
                    if (upload.LastRequestedSector > -1)
                        fixed(UnsafeColor *pNextSending = &nextSendingColors[0])
                        {
                            int n;

                            for (y = 0; y < bitmap.Height; y++)
                            {
                                pbmd = (byte *)bmd.Scan0 + y * bmd.Stride + requestedLeft * 3;
                                for (n = 0, x = requestedLeft; n < progressWidth && x < bitmap.Width; n++, x++)
                                {
                                    c = &pNextSending[y];
                                    *pbmd++ = c->B;
                                    *pbmd++ = c->G;
                                    *pbmd++ = c->R;
                                }
                            }
                        }
                }
            }
            else
            {
                both = Color.FromArgb(0, 0, 0);
                Color c;
                fixed(bool *pSector = &progressBarMap[0])
                {
                    bool *p;

                    for (y = 0; y < bitmap.Height; y++)
                    {
                        p    = pSector;
                        pbmd = (byte *)bmd.Scan0 + y * bmd.Stride;
                        for (x = 0; x < bitmap.Width; x++)
                        {
                            c = *p++ ? both : neither;
                            *pbmd++ = c.B;
                            *pbmd++ = c.G;
                            *pbmd++ = c.R;
                        }
                    }
                }

                if (upload.IsActive)
                {
                    if (upload.LastSentSector > -1)
                    {
                        c = sending;
                        int n;
                        for (y = 0; y < bitmap.Height; y++)
                        {
                            pbmd = (byte *)bmd.Scan0 + y * bmd.Stride + sentLeft * 3;
                            for (n = 0, x = sentLeft; n < progressWidth && x < bitmap.Width; n++, x++)
                            {
                                *pbmd++ = c.B;
                                *pbmd++ = c.G;
                                *pbmd++ = c.R;
                            }
                        }
                    }
                    if (upload.LastRequestedSector > -1)
                    {
                        c = nextSending;
                        int n;
                        for (y = 0; y < bitmap.Height; y++)
                        {
                            pbmd = (byte *)bmd.Scan0 + y * bmd.Stride + requestedLeft * 3;
                            for (n = 0, x = requestedLeft; n < progressWidth && x < bitmap.Width; n++, x++)
                            {
                                *pbmd++ = c.B;
                                *pbmd++ = c.G;
                                *pbmd++ = c.R;
                            }
                        }
                    }
                }
            }
            bitmap.UnlockBits(bmd);
            return(bitmap);
        }
コード例 #2
0
        public static unsafe Image GetProgressBar(Upload upload, int width, int height)
        {
            if (upload == null)
                throw new ArgumentNullException("upload");
            if (width < 0)
                throw new ArgumentOutOfRangeException("width");
            if (height < 0)
                throw new ArgumentOutOfRangeException("height");

            Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            BitmapData bmd = bitmap.LockBits(new Rectangle(new Point(0, 0), bitmap.Size), ImageLockMode.ReadOnly, bitmap.PixelFormat);
            byte* pbmd;
            int y;
            int x;
            int sentLeft = (int)Math.Floor((double)Math.Max(upload.LastSentSector, 0) / (double)upload.Sectors * (double)bitmap.Width);
            int requestedLeft = (int)Math.Floor((double)Math.Max(upload.LastRequestedSector, 0) / (double)upload.Sectors * (double)bitmap.Width);
            int progressWidth = (int)Math.Ceiling((double)bitmap.Width / (double)upload.Sectors);
            bool[] progressBarMap = new bool[width];
            ScaleLinear(upload.SectorsMap, (int)upload.Sectors, progressBarMap);
            Color neither = Color.FromArgb(224, 224, 224);
            Color both;
            Color sending = Color.FromArgb(0, 150, 0);
            Color nextSending = Color.FromArgb(255, 208, 0);
            if (bool.Parse(Settings.Instance["ProgressBarsHaveShadow"]))
            {
                both = Color.FromArgb(104, 104, 104);
                double[] shadows = ComputeShadows(bitmap.Height, int.Parse(Settings.Instance["ProgressBarsShadow"]));
                UnsafeColor[] neitherColors = ComputeColors(neither, bitmap.Height, shadows);
                UnsafeColor[] bothColors = ComputeColors(both, bitmap.Height, shadows);
                UnsafeColor[] sendingColors = ComputeColors(sending, bitmap.Height, shadows);
                UnsafeColor[] nextSendingColors = ComputeColors(nextSending, bitmap.Height, shadows);
                UnsafeColor* c;
                fixed (bool* pSector = &progressBarMap[0])
                {
                    fixed (UnsafeColor* pBoth = &bothColors[0], pNeither = &neitherColors[0])
                    {
                        bool* p;
                        for (y = 0; y < bitmap.Height; y++)
                        {
                            p = pSector;
                            pbmd = (byte*)bmd.Scan0 + y * bmd.Stride;
                            for (x = 0; x < bitmap.Width; x++)
                            {
                                c = *p++ ? &pBoth[y] : &pNeither[y];
                                *pbmd++ = c->B;
                                *pbmd++ = c->G;
                                *pbmd++ = c->R;
                            }
                        }
                    }
                }
                if (upload.IsActive)
                {
                    if (upload.LastSentSector > -1)
                        fixed (UnsafeColor* pSending = &sendingColors[0])
                        {
                            int n;
                            for (y = 0; y < bitmap.Height; y++)
                            {
                                pbmd = (byte*)bmd.Scan0 + y * bmd.Stride + sentLeft * 3;
                                for (n = 0, x = sentLeft; n < progressWidth && x < bitmap.Width; n++, x++)
                                {
                                    c = &pSending[y];
                                    *pbmd++ = c->B;
                                    *pbmd++ = c->G;
                                    *pbmd++ = c->R;
                                }
                            }
                        }
                    if (upload.LastRequestedSector > -1)
                        fixed (UnsafeColor* pNextSending = &nextSendingColors[0])
                        {
                            int n;
                            for (y = 0; y < bitmap.Height; y++)
                            {
                                pbmd = (byte*)bmd.Scan0 + y * bmd.Stride + requestedLeft * 3;
                                for (n = 0, x = requestedLeft; n < progressWidth && x < bitmap.Width; n++, x++)
                                {
                                    c = &pNextSending[y];
                                    *pbmd++ = c->B;
                                    *pbmd++ = c->G;
                                    *pbmd++ = c->R;
                                }
                            }
                        }
                }
            }
            else
            {
                both = Color.FromArgb(0, 0, 0);
                Color c;
                fixed (bool* pSector = &progressBarMap[0])
                {
                    bool* p;
                    for (y = 0; y < bitmap.Height; y++)
                    {
                        p = pSector;
                        pbmd = (byte*)bmd.Scan0 + y * bmd.Stride;
                        for (x = 0; x < bitmap.Width; x++)
                        {
                            c = *p++ ? both : neither;
                            *pbmd++ = c.B;
                            *pbmd++ = c.G;
                            *pbmd++ = c.R;
                        }
                    }
                }
                if (upload.IsActive)
                {
                    if (upload.LastSentSector > -1)
                    {
                        c = sending;
                        int n;
                        for (y = 0; y < bitmap.Height; y++)
                        {
                            pbmd = (byte*)bmd.Scan0 + y * bmd.Stride + sentLeft * 3;
                            for (n = 0, x = sentLeft; n < progressWidth && x < bitmap.Width; n++, x++)
                            {
                                *pbmd++ = c.B;
                                *pbmd++ = c.G;
                                *pbmd++ = c.R;
                            }
                        }
                    }
                    if (upload.LastRequestedSector > -1)
                    {
                        c = nextSending;
                        int n;
                        for (y = 0; y < bitmap.Height; y++)
                        {
                            pbmd = (byte*)bmd.Scan0 + y * bmd.Stride + requestedLeft * 3;
                            for (n = 0, x = requestedLeft; n < progressWidth && x < bitmap.Width; n++, x++)
                            {
                                *pbmd++ = c.B;
                                *pbmd++ = c.G;
                                *pbmd++ = c.R;
                            }
                        }
                    }
                }
            }
            bitmap.UnlockBits(bmd);
            return bitmap;
        }