Example #1
0
        private void TransferChannel(Bitmap srcBitmap, ChannelARGB srcChannel, Bitmap dstBitmap, ChannelARGB dstChannel)
        {
            var srcData = srcBitmap.LockBits(new Rectangle(0, 0, srcBitmap.Width, srcBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            try
            {
                var dstData = dstBitmap.LockBits(new Rectangle(0, 0, dstBitmap.Width, dstBitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
                try
                {
                    var ret = WallSwitchImgProc.TransferChannel32(srcData.Scan0, (int)srcChannel, srcData.Stride,
                                                                  dstData.Scan0, (int)dstChannel, dstData.Stride,
                                                                  srcBitmap.Width, srcBitmap.Height);
                    if (ret != 0)
                    {
                        Log.Write(LogLevel.Error, "Error when transferring bitmap channel: {0}", ret);
                    }
                }
                finally
                {
                    dstBitmap.UnlockBits(dstData);
                }
            }
            finally
            {
                srcBitmap.UnlockBits(srcData);
            }
        }
Example #2
0
        private static Image LoadWebpFile(string pathName)
        {
            var fileBytes = File.ReadAllBytes(pathName);
            var fileLen   = fileBytes.Length;
            var filePtr   = Marshal.AllocHGlobal(fileLen);

            try
            {
                Marshal.Copy(fileBytes, 0, filePtr, fileBytes.Length);

                var packedSize = WallSwitchImgProc.webpGetSize(filePtr, (uint)fileLen);
                if (packedSize == 0)
                {
                    // This might not be a webp file, but just has the wrong extension. Try loading conventionally.
                    try
                    {
                        using (var stream = new MemoryStream(fileBytes))
                        {
                            return(Image.FromStream(stream));
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex, "Webp file could not be loaded as a webp or conventional image: {0}", pathName);
                        throw new WebpLoadException($"Invalid webp image: {pathName}");
                    }
                }

                var width  = (int)(packedSize >> 16);
                var height = (int)(packedSize & 0xffff);
                var bmp    = new Bitmap(width, height);

                var bmpData = bmp.LockBits(new Rectangle(0, 0, width, height),
                                           System.Drawing.Imaging.ImageLockMode.ReadWrite,
                                           System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                try
                {
                    if (WallSwitchImgProc.webpLoadARGB(filePtr, (uint)fileLen, bmpData.Scan0,
                                                       (uint)(bmpData.Stride * bmpData.Height), (uint)bmpData.Stride) != 0)
                    {
                        throw new WebpLoadException($"Failed to decode webp image: {pathName}");
                    }
                }
                finally
                {
                    bmp.UnlockBits(bmpData);
                }

                return(bmp);
            }
            finally
            {
                Marshal.FreeHGlobal(filePtr);
            }
        }
Example #3
0
        private void BlurImage(Bitmap img, int blurDist)
        {
            var imgData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            try
            {
                WallSwitchImgProc.BlurImage(imgData.Scan0, img.Width, img.Height, 0, imgData.Stride, blurDist);
            }
            finally
            {
                img.UnlockBits(imgData);
            }
        }
Example #4
0
 public static void ExploreFile(string fileName)
 {
     WallSwitchImgProc.ShowFileInExplorer(fileName);
 }