当前位置: 首页>>代码示例>>C#>>正文


C# System.Drawing.Bitmap.GetHicon方法代码示例

本文整理汇总了C#中System.Drawing.Bitmap.GetHicon方法的典型用法代码示例。如果您正苦于以下问题:C# System.Drawing.Bitmap.GetHicon方法的具体用法?C# System.Drawing.Bitmap.GetHicon怎么用?C# System.Drawing.Bitmap.GetHicon使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Drawing.Bitmap的用法示例。


在下文中一共展示了System.Drawing.Bitmap.GetHicon方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GDriveForceUpdateContext

        public GDriveForceUpdateContext()
        {
            running = true;

            mComponents = new System.ComponentModel.Container();

            mNotifyIcon = new NotifyIcon(this.mComponents);
            //System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
            //Stream myStream = myAssembly.GetManifestResourceStream("icon.png");
            Stream s = GetType().Assembly.GetManifestResourceStream("GDriveForceUpdate.icon.png");
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(s);
            s.Close();
            mNotifyIcon.Icon = System.Drawing.Icon.FromHandle(bmp.GetHicon());
            mNotifyIcon.Text = "GDrive Force Update";
            mNotifyIcon.Visible = true;

            mContextMenu = new ContextMenuStrip();
            mDisplayForm = new ToolStripMenuItem();
            mExitApplication = new ToolStripMenuItem();
            pauseUpdate = new ToolStripMenuItem();

            mNotifyIcon.ContextMenuStrip = mContextMenu;

            mDisplayForm.Text = "About...";
            mDisplayForm.Click += new EventHandler(mDisplayForm_Click);
            mContextMenu.Items.Add(mDisplayForm);

            pauseUpdate.Text = "Pause";
            pauseUpdate.Click += new EventHandler(mPauseUpdate_Click);
            mContextMenu.Items.Add(pauseUpdate);

            mExitApplication.Text = "Exit";
            mExitApplication.Click += new EventHandler(mExitApplication_Click);
            mContextMenu.Items.Add(mExitApplication);

            m_oWorker = new BackgroundWorker();
            m_oWorker.DoWork += new DoWorkEventHandler(m_oWorker_DoWork);
            //m_oWorker.ProgressChanged += new ProgressChangedEventHandler(m_oWorker_ProgressChanged);
            //m_oWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_oWorker_RunWorkerCompleted);
            m_oWorker.WorkerReportsProgress = true;
            m_oWorker.WorkerSupportsCancellation = true;
            m_oWorker.RunWorkerAsync();
        }
开发者ID:nicola-amatucci,项目名称:GDriveForceUpdate,代码行数:43,代码来源:GDriveForceUpdateContext.cs

示例2: SaveBmpImageAsIcon

 static void SaveBmpImageAsIcon(string imagePath, byte[] imageBytes, System.Drawing.Image image) {
     using(System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) {
         using(System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap))
             graphics.DrawImage(image, 0, 0, image.Width, image.Height);
         IntPtr iconHandle = bitmap.GetHicon();
         try {
             using(System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(iconHandle)) {
                 WriteIconToFile(imagePath, f => icon.Save(f));
             }
         } finally {
             DestroyIcon(iconHandle);
         }
     }
 }
开发者ID:sk8tz,项目名称:DevExpress.Mvvm.Free,代码行数:14,代码来源:IconStorage.cs

示例3: MakeIcon

        /// <summary>
        /// Converts an image into an icon.
        /// </summary>
        /// <param name="img">The image that shall become an icon</param>
        /// <param name="size">The width and height of the icon. Standard
        /// sizes are 16x16, 32x32, 48x48, 64x64.</param>
        /// <param name="keepAspectRatio">Whether the image should be squashed into a
        /// square or whether whitespace should be put around it.</param>
        /// <returns>An icon!!</returns>
        public static System.Drawing.Icon MakeIcon(System.Drawing.Image img, int size, bool keepAspectRatio)
        {
            System.Drawing.Bitmap square = new System.Drawing.Bitmap(size, size); // create new bitmap
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(square); // allow drawing to it

            int x, y, w, h; // dimensions for new image

            if (!keepAspectRatio || img.Height == img.Width)
            {
                // just fill the square
                x = y = 0; // set x and y to 0
                w = h = size; // set width and height to size
            }
            else
            {
                // work out the aspect ratio
                float r = (float)img.Width / (float)img.Height;

                // set dimensions accordingly to fit inside size^2 square
                if (r > 1)
                { // w is bigger, so divide h by r
                    w = size;
                    h = (int)((float)size / r);
                    x = 0; y = (size - h) / 2; // center the image
                }
                else
                { // h is bigger, so multiply w by r
                    w = (int)((float)size * r);
                    h = size;
                    y = 0; x = (size - w) / 2; // center the image
                }
            }

            // make the image shrink nicely by using HighQualityBicubic mode
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(img, x, y, w, h); // draw image with specified dimensions
            g.Flush(); // make sure all drawing operations complete before we get the icon

            // following line would work directly on any image, but then
            // it wouldn't look as nice.
            return System.Drawing.Icon.FromHandle(square.GetHicon());
        }
开发者ID:EgyFalseX,项目名称:Winform,代码行数:51,代码来源:Program.cs

示例4: ConvertToCursor

        /// <summary>
        /// This converts a wpf visual into a mouse cursor (call this.RenderControl to get the bitmapsource)
        /// NOTE: Needs to be a standard size (16x16, 32x32, etc)
        /// TODO: Fix this for semitransparency
        /// </summary>
        /// <remarks>
        /// Got this here:
        /// http://stackoverflow.com/questions/46805/custom-cursor-in-wpf
        /// </remarks>
        public static Cursor ConvertToCursor(BitmapSource bitmapSource, Point hotSpot)
        {
            int width = bitmapSource.PixelWidth;
            int height = bitmapSource.PixelHeight;

            // Get a byte array
            int stride = (width * bitmapSource.Format.BitsPerPixel + 7) / 8;		//http://msdn.microsoft.com/en-us/magazine/cc534995.aspx
            byte[] bytes = new byte[stride * height];

            bitmapSource.CopyPixels(bytes, stride, 0);

            // Convert to System.Drawing.Bitmap
            var bitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
            for (int y = 0; y < height; y++)
            {
                int rowOffset = y * stride;
                int yOffset = y * width;

                for (int x = 0; x < width; x++)
                {
                    int offset = rowOffset + (x * 4);		// this is assuming that bitmap.Format.BitsPerPixel is 32, which would be four bytes per pixel

                    bitmap.SetPixel(x, y, System.Drawing.Color.FromArgb(bytes[offset + 3], bytes[offset + 2], bytes[offset + 1], bytes[offset + 0]));
                    //bitmap.SetPixel(x, y, System.Drawing.Color.FromArgb(64, 255, 0, 0));
                }
            }

            // Save to .ico format
            MemoryStream stream = new MemoryStream();
            System.Drawing.Icon.FromHandle(bitmap.GetHicon()).Save(stream);

            // Convert saved file into .cur format
            stream.Seek(2, SeekOrigin.Begin);
            stream.WriteByte(2);        // convert to cur format

            //stream.Seek(8, SeekOrigin.Begin);     // trying to wipe out the color pallete to be able to support transparency, but has no effect
            //stream.WriteByte(0);
            //stream.WriteByte(0);

            stream.Seek(10, SeekOrigin.Begin);
            stream.WriteByte((byte)(int)(hotSpot.X * width));
            stream.WriteByte((byte)(int)(hotSpot.Y * height));
            stream.Seek(0, SeekOrigin.Begin);

            // Construct Cursor
            return new Cursor(stream);
        }
开发者ID:charlierix,项目名称:AsteroidMiner,代码行数:56,代码来源:UtilityWPF.cs

示例5: InitTrayIcon

        void InitTrayIcon()
        {
            BitmapImage bi = new BitmapImage( new Uri( "pack://application:,,,/TruePass;component/icon_24x24.png" ) );

            MemoryStream mse = new MemoryStream();
            PngBitmapEncoder
                be = new PngBitmapEncoder();
                be.Frames.Add( BitmapFrame.Create( bi ) );
                be.Save( mse );

            System.Drawing.Bitmap b = new System.Drawing.Bitmap( mse );

            System.Windows.Forms.MenuItem
                mi = new System.Windows.Forms.MenuItem( "Exit" );
                mi.Click += delegate( object sender, EventArgs e )
                {
                    _paused = true;
                    Closing -= Window_Closing;

                    Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                    Application.Current.Shutdown();
                };

            _notifyIcon = new System.Windows.Forms.NotifyIcon();
            _notifyIcon.Icon = System.Drawing.Icon.FromHandle( b.GetHicon() );
            _notifyIcon.Visible = true;
            _notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu( new System.Windows.Forms.MenuItem[] { mi } );
            _notifyIcon.DoubleClick += new EventHandler( NotifyIcon_DoubleClick );
        }
开发者ID:nic0lae,项目名称:TruePass,代码行数:29,代码来源:MainWindow.xaml.cs

示例6: GetIconHandle

        public static IntPtr GetIconHandle(ImageSource source)
        {
            System.Windows.Media.Imaging.BitmapFrame frame = source as System.Windows.Media.Imaging.BitmapFrame;

            if (frame != null && frame.Decoder.Frames.Count > 0)
            {
                frame = frame.Decoder.Frames[0];

                int width = frame.PixelWidth;
                int height = frame.PixelHeight;
                int stride = width * ((frame.Format.BitsPerPixel + 7) / 8);

                Byte[] bits = new Byte[height * stride];

                frame.CopyPixels(bits, stride, 0);

                GCHandle gcHandle = GCHandle.Alloc(bits, GCHandleType.Pinned);

                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(
                    width,
                    height,
                    stride,
                    System.Drawing.Imaging.PixelFormat.Format32bppPArgb,
                    gcHandle.AddrOfPinnedObject());

                IntPtr hIcon = bitmap.GetHicon();

                gcHandle.Free();

                return hIcon;
            }

            return IntPtr.Zero;
        }
开发者ID:kav-it,项目名称:SharpLib,代码行数:34,代码来源:Gui.cs

示例7: parser_NotifyIconUpdated

        void parser_NotifyIconUpdated(object sender, GoogleDocsNotifier.Events.NotifyIconUpdatedEventArgs e)
        {
            if (e.NumOfUnviewedDocuments > 0)
            {
                System.Drawing.Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
                //The original icon for the Google Docs Notifier.
                System.Drawing.Image originalAppIcon =
                System.Drawing.Icon.ExtractAssociatedIcon(
                System.Windows.Forms.Application.ExecutablePath).ToBitmap();
                //Create a bitmap and draw the number of unviewed documents on it.
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(16, 16);
                System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);
                graphics.DrawImage(originalAppIcon, 0, 0, 16, 16);
                graphics.DrawString(e.NumOfUnviewedDocuments.ToString(), new System.Drawing.Font("Helvetica", 6), brush, 3, 3);
                graphics.Save();
                //Convert the bitmap with text to an Icon.
                IntPtr hIcon = bitmap.GetHicon();
                System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(hIcon);
                trayNotifyIcon.Icon = icon;
                //Dispose the graphics.
                graphics.Dispose();
                trayNotifyIcon.Text = e.NumOfUnviewedDocuments.ToString() + " unread documents";
            }
            else
            {
                trayNotifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
                trayNotifyIcon.Text = "No unread document";
            }

            //Show the balloon tooltip.
            if (e.IsDisplayTooltip)
            {
                //Title of the balloon tooltip.
                trayNotifyIcon.BalloonTipTitle = e.Title;
                trayNotifyIcon.BalloonTipText = e.Message;
                trayNotifyIcon.ShowBalloonTip(500);
            }
        }
开发者ID:emilson18,项目名称:googledocsnotifier,代码行数:38,代码来源:MainWindow.xaml.cs

示例8: _CreateCursorFromCanvas

        /// <summary>
        /// Create cursor from canvas.
        /// </summary>
        /// <param name="canvas">Input canvas.</param>
        /// <param name="hotSpotPoint">Hotspot cursor point.</param>
        /// <returns>Created cursor.</returns>
        private System.Windows.Forms.Cursor _CreateCursorFromCanvas(Canvas canvas, System.Drawing.Point hotSpotPoint)
        {
            // Render canvas to bitmap.
            RenderTargetBitmap targetBitmap = new RenderTargetBitmap((int)canvas.ActualWidth, (int)canvas.ActualHeight, BASIC_DPI, BASIC_DPI, PixelFormats.Pbgra32);
            targetBitmap.Render(canvas);

            // Create PNG encoder.
            PngBitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(targetBitmap));

            // Render target bitmap to System.Drawing bitmap.
            System.Drawing.Bitmap bmp;
            using (MemoryStream stream = new MemoryStream())
            {
                encoder.Save(stream);
                bmp = new System.Drawing.Bitmap(stream);
            }

            // Create cursor.
            WinAPIHelpers.IconInfo iconInfo = new WinAPIHelpers.IconInfo();
            IntPtr hIcon = bmp.GetHicon();
            WinAPIHelpers.GetIconInfo(hIcon, ref iconInfo);
            iconInfo.xHotspot = hotSpotPoint.X;
            iconInfo.yHotspot = hotSpotPoint.Y;
            iconInfo.fIcon = false;
            IntPtr cursorHandle = WinAPIHelpers.CreateIconIndirect(ref iconInfo);

            // Destroy icon obtained from GetHicon method.
            if (hIcon != IntPtr.Zero)
                WinAPIHelpers.DestroyIcon(hIcon);

            // Free resources.
            if (iconInfo.hbmColor != IntPtr.Zero)
                WinAPIHelpers.DeleteObject(iconInfo.hbmColor);
            if (iconInfo.hbmMask != IntPtr.Zero)
                WinAPIHelpers.DeleteObject(iconInfo.hbmMask);

            System.Windows.Forms.Cursor cursor = null;
            if (cursorHandle != null)
                cursor = new System.Windows.Forms.Cursor(cursorHandle);

            return cursor;
        }
开发者ID:erindm,项目名称:route-planner-csharp,代码行数:49,代码来源:DragDropFeedbacker.cs


注:本文中的System.Drawing.Bitmap.GetHicon方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。