private Image GetRatingImage(Download.Source source) { if (source == null) { throw new ArgumentNullException("source"); } switch (source.Rating) { case 1: return(Properties.Resources.rating1_16x16); case 2: return(Properties.Resources.rating2_16x16); case 3: return(Properties.Resources.rating3_16x16); default: return(Properties.Resources.rating0_16x16); } }
public unsafe static Image GetProgressBar(Download download, Download.Source source, int width, int height) { if (download == null) { throw new ArgumentNullException("download"); } if (source == null) { throw new ArgumentNullException("source"); } 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 receivedLeft = (int)Math.Floor((double)Math.Max(source.LastReceivedSector, 0) / (double)download.Sectors * (double)bitmap.Width); int requestedLeft = (int)Math.Floor((double)Math.Max(source.LastRequestedSector, 0) / (double)download.Sectors * (double)bitmap.Width); int progressWidth = (int)Math.Ceiling((double)bitmap.Width / (double)download.Sectors); bool[] downloadProgressBarMap = new bool[width]; ScaleLinear(download.SectorsMap, (int)download.Sectors, downloadProgressBarMap); bool[] sourceProgressBarMap = new bool[width]; if (!source.IsComplete) { ScaleLinear(source.SectorsMap, (int)download.Sectors, sourceProgressBarMap); } else { for (int n = 0; n < sourceProgressBarMap.Length; n++) { sourceProgressBarMap[n] = true; } } Color neither; Color both; //Color clientOnly = Color.FromArgb(0, 100, 255); Color clientOnly; Color pending = Color.FromArgb(0, 150, 0); Color nextPending = Color.FromArgb(255, 208, 0); if (bool.Parse(Settings.Instance["ProgressBarsHaveShadow"])) { neither = Color.FromArgb(240, 240, 240); both = Color.FromArgb(104, 104, 104); clientOnly = Color.FromArgb(0, 210 - 22 * (download.Sources.Count - 1) < 0 ? 0 : 210 - 22 * (download.Sources.Count - 1), 255); 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[] clientOnlyColors = ComputeColors(clientOnly, bitmap.Height, shadows); UnsafeColor[] pendingColors = ComputeColors(pending, bitmap.Height, shadows); UnsafeColor[] nextPendingColors = ComputeColors(nextPending, bitmap.Height, shadows); UnsafeColor * c; fixed(bool *pDownloadSector = &downloadProgressBarMap[0], pSourceSector = &sourceProgressBarMap[0]) { fixed(UnsafeColor *pNeither = &neitherColors[0], pBoth = &bothColors[0], pClientOnly = &clientOnlyColors[0]) { bool *pDownload; bool *pSource; for (y = 0; y < bitmap.Height; y++) { pDownload = pDownloadSector; pSource = pSourceSector; pbmd = (byte *)bmd.Scan0 + y * bmd.Stride; for (x = 0; x < bitmap.Width; x++) { c = *pDownload && *pSource ? &pBoth[y] : (*pSource ? (source.State != Download.SourceState.NotNeeded ? &pClientOnly[y] : &pNeither[y]) : &pNeither[y]); pDownload++; pSource++; *pbmd++ = c->B; *pbmd++ = c->G; *pbmd++ = c->R; } } } } if (source.State == Download.SourceState.Active) { if (source.LastReceivedSector > -1) fixed(UnsafeColor *pPending = &pendingColors[0]) { int n; for (y = 0; y < bitmap.Height; y++) { pbmd = (byte *)bmd.Scan0 + y * bmd.Stride + receivedLeft * 3; for (n = 0, x = receivedLeft; n < progressWidth && x < bitmap.Width; n++, x++) { c = &pPending[y]; *pbmd++ = c->B; *pbmd++ = c->G; *pbmd++ = c->R; } } } if (source.LastRequestedSector > -1) fixed(UnsafeColor *pNextPending = &nextPendingColors[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 = &pNextPending[y]; *pbmd++ = c->B; *pbmd++ = c->G; *pbmd++ = c->R; } } } } } else { neither = Color.FromArgb(224, 224, 224); both = Color.FromArgb(0, 0, 0); clientOnly = Color.FromArgb(64, 169 - 11 * (download.Sources.Count - 1) < 64 ? 64 : 169 - 11 * (download.Sources.Count - 1), 191); Color c; fixed(bool *pDownloadSector = &downloadProgressBarMap[0], pSourceSector = &sourceProgressBarMap[0]) { bool *pDownload; bool *pSource; for (y = 0; y < bitmap.Height; y++) { pDownload = pDownloadSector; pSource = pSourceSector; pbmd = (byte *)bmd.Scan0 + y * bmd.Stride; for (x = 0; x < bitmap.Width; x++) { c = *pDownload && *pSource ? both : (*pSource ? (source.State != Download.SourceState.NotNeeded ? clientOnly : neither) : neither); pDownload++; pSource++; *pbmd++ = c.B; *pbmd++ = c.G; *pbmd++ = c.R; } } } if (source.State == Download.SourceState.Active) { if (source.LastReceivedSector > -1) { c = pending; int n; for (y = 0; y < bitmap.Height; y++) { pbmd = (byte *)bmd.Scan0 + y * bmd.Stride + receivedLeft * 3; for (n = 0, x = receivedLeft; n < progressWidth && x < bitmap.Width; n++, x++) { *pbmd++ = c.B; *pbmd++ = c.G; *pbmd++ = c.R; } } } if (source.LastRequestedSector > -1) { c = nextPending; 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); }