walk icon indicating copy to clipboard operation
walk copied to clipboard

How can I embed an image within the exe file

Open swagar opened this issue 4 years ago • 8 comments

How can I embed an image file within the exe file as a resource like the manifest. At the moment I need to put the original image file along with the exe file and this is a problem since the program doesn't run without the image file.

swagar avatar Jan 07 '20 20:01 swagar

I had the same problem with the notifyicon example.

with version 1.16 we can simply insert the icon into the *.exe

// Copyright 2011 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"bytes"
	"image"
	"log"

	_ "embed"

	"github.com/lxn/walk"
)

/// NEW since go 1.16
//go:embed stop.ico
var stop []byte

func main() {
	// We need either a walk.MainWindow or a walk.Dialog for their message loop.
	// We will not make it visible in this example, though.
	mw, err := walk.NewMainWindow()
	if err != nil {
		log.Fatal(err)
	}

	// We load our icon from a file.
	img, _, err := image.Decode(bytes.NewReader(stop)) // NEW
	if err != nil {
		log.Fatal(err)
	}
	icon, err := walk.NewIconFromImageForDPI(img, 96) // NEW
	if err != nil {
		log.Fatal(err)
	}
	// icon, err := walk.Resources.Icon("../img/stop.ico") // OLD
	// if err != nil {
	// 	log.Fatal(err)
	// }

	// Create the notify icon and make sure we clean it up on exit.
	ni, err := walk.NewNotifyIcon(mw)
	if err != nil {
		log.Fatal(err)
	}
	defer ni.Dispose()

	// Set the icon and a tool tip text.
	if err := ni.SetIcon(icon); err != nil {
		log.Fatal(err)
	}
	if err := ni.SetToolTip("Click for info or use the context menu to exit."); err != nil {
		log.Fatal(err)
	}

	// When the left mouse button is pressed, bring up our balloon.
	ni.MouseDown().Attach(func(x, y int, button walk.MouseButton) {
		if button != walk.LeftButton {
			return
		}

		if err := ni.ShowCustom(
			"Walk NotifyIcon Example",
			"There are multiple ShowX methods sporting different icons.",
			icon); err != nil {

			log.Fatal(err)
		}
	})

	// We put an exit action into the context menu.
	exitAction := walk.NewAction()
	if err := exitAction.SetText("E&xit"); err != nil {
		log.Fatal(err)
	}
	exitAction.Triggered().Attach(func() { walk.App().Exit(0) })
	if err := ni.ContextMenu().Actions().Add(exitAction); err != nil {
		log.Fatal(err)
	}

	// The notify icon is hidden initially, so we have to make it visible.
	if err := ni.SetVisible(true); err != nil {
		log.Fatal(err)
	}

	// Now that the icon is visible, we can bring up an info balloon.
	if err := ni.ShowInfo("Walk NotifyIcon Example", "Click the icon to show again."); err != nil {
		log.Fatal(err)
	}

	// Run the message loop.
	mw.Run()
}

I hope it helps

spddl avatar Mar 15 '21 13:03 spddl

Thank you @spddl, I was just looking for how to do this! :)

roffe avatar Apr 05 '21 12:04 roffe

Thank you @spddl, I was also just looking for how to do this! :)

GodGavin avatar Mar 15 '22 04:03 GodGavin

Thank you @spddl, I was also just looking for how to do this! :)

but image cant deode a .ico file, false:image: unknown format ,how to sovle this problem ?@@spddl

GodGavin avatar Mar 15 '22 07:03 GodGavin

@GodGavin You can use png image to convert to ico image。 show my code. QQ图片20220625003806

529124368 avatar Jun 24 '22 15:06 529124368

Thank you @spddl, I was also just looking for how to do this! :)

but image cant deode a .ico file, false:image: unknown format ,how to sovle this problem ?@@spddl

Thank you @spddl, I was also just looking for how to do this! :)

but image cant deode a .ico file, false:image: unknown format ,how to sovle this problem ?@@spddl

have you finger out this problem?

82kg avatar Jul 14 '22 05:07 82kg

Because of the "image" can't parse .ico file. Just use this package:

go get github.com/mat/besticon/ico
// Copyright 2011 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"bytes"
	"log"

	_ "embed"

	"github.com/lxn/walk"
	"github.com/mat/besticon/ico"
)

// / NEW since go 1.16
//
//go:embed favicon.ico
var stop []byte

func main() {
	// We need either a walk.MainWindow or a walk.Dialog for their message loop.
	// We will not make it visible in this example, though.
	mw, err := walk.NewMainWindow()
	if err != nil {
		log.Fatal(err)
	}

	// We load our icon from a file.
	img, err := ico.Decode(bytes.NewReader(stop)) // NEW parse ico file.
	if err != nil {
		log.Fatal(err)
	}
	icon, err := walk.NewIconFromImageForDPI(img, 96) // NEW
	if err != nil {
		log.Fatal(err)
	}
	// icon, err := walk.Resources.Icon("../img/stop.ico") // OLD
	// if err != nil {
	// 	log.Fatal(err)
	// }

	// Create the notify icon and make sure we clean it up on exit.
	ni, err := walk.NewNotifyIcon(mw)
	if err != nil {
		log.Fatal(err)
	}
	defer ni.Dispose()

	// Set the icon and a tool tip text.
	if err := ni.SetIcon(icon); err != nil {
		log.Fatal(err)
	}
	if err := ni.SetToolTip("Click for info or use the context menu to exit."); err != nil {
		log.Fatal(err)
	}

	// When the left mouse button is pressed, bring up our balloon.
	ni.MouseDown().Attach(func(x, y int, button walk.MouseButton) {
		if button != walk.LeftButton {
			return
		}

		if err := ni.ShowCustom(
			"Walk NotifyIcon Example",
			"There are multiple ShowX methods sporting different icons.",
			icon); err != nil {

			log.Fatal(err)
		}
	})

	// We put an exit action into the context menu.
	exitAction := walk.NewAction()
	if err := exitAction.SetText("E&xit"); err != nil {
		log.Fatal(err)
	}
	exitAction.Triggered().Attach(func() { walk.App().Exit(0) })
	if err := ni.ContextMenu().Actions().Add(exitAction); err != nil {
		log.Fatal(err)
	}

	// The notify icon is hidden initially, so we have to make it visible.
	if err := ni.SetVisible(true); err != nil {
		log.Fatal(err)
	}

	// Now that the icon is visible, we can bring up an info balloon.
	if err := ni.ShowInfo("Walk NotifyIcon Example", "Click the icon to show again."); err != nil {
		log.Fatal(err)
	}

	// Run the message loop.
	mw.Run()
}

wilon avatar Nov 15 '23 01:11 wilon