Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: Embed images in dll's  (Read 2674 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« on: February 11, 2009, 07:03:37 PM »

There wasn't a lot of documentation about embedding images to a dll so i've written my own demo to find out how it works. The demo checks if the entered number is above 1000 and below 100000 just to show different images. Ive attached the project below.

This is a class that handles the events. (i know, not very well written but heck, i'm sure you can refactor it yourself)

Code
GeSHi (csharp):
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11.  
  12. namespace EmbeddedImageDemo
  13. {
  14.    public partial class _Default : System.Web.UI.Page
  15.    {
  16.        protected void Page_Load(object sender, EventArgs e)
  17.        {
  18.  
  19.            /*
  20.             Image CalendarImage = new Image();
  21.  
  22.             CalendarImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(
  23.              this.GetType(),
  24.                 //Replace following with the string entered in AssemblyInfo.cs
  25.              "EmbeddedImageDemo.Test.png");
  26.             */
  27.  
  28.            Image img = new Image();
  29.  
  30.           // img.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "EmbeddedImageDemo.Test.png");
  31.           // Image1.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "EmbeddedImageDemo.Test.png");
  32.  
  33.            //works
  34.           //Image1.ImageUrl = Page.ClientScript.GetWebResourceUrl(typeof(_Default), "EmbeddedImageDemo.Test.png");
  35.  
  36.        }
  37.  
  38.        protected void Button1_Click(object sender, EventArgs e)
  39.        {
  40.            try
  41.            {
  42.                Int32 largeNumber = Convert.ToInt32(TextBox1.Text);
  43.  
  44.                if (largeNumber < 1000)
  45.                {
  46.                    Image1.ImageUrl = Page.ClientScript.GetWebResourceUrl(typeof(_Default), "EmbeddedImageDemo.Resources.cancel.png");
  47.                    Label1.Text = "Try again";
  48.                }
  49.  
  50.                else if (largeNumber >= 1000 && largeNumber <= 100000)
  51.                {
  52.                    Image1.ImageUrl = Page.ClientScript.GetWebResourceUrl(typeof(_Default), "EmbeddedImageDemo.Resources.button_ok.png");
  53.                    //Label1.Text = "value: " + TextBox1.Text;
  54.                    Label1.Text = "Correct";
  55.                }
  56.  
  57.                else
  58.                {
  59.                    Image1.ImageUrl = Page.ClientScript.GetWebResourceUrl(typeof(_Default), "EmbeddedImageDemo.Resources.help.png");
  60.                    Label1.Text = "Value out of range";
  61.                }
  62.  
  63.            }
  64.            catch (Exception)
  65.            {
  66.                Label1.Text = "Not a valid number";
  67.            }
  68.  
  69.  
  70.        }
  71.  
  72.  
  73.    }
  74. }
  75.  
Created by GeSHI 1.0.7.20

In short you need to do a few things to make it possible to embed images into a dll file.

1) add an image to your (web) project
2) select the image and go to property -> "Build Action" and choose "Embedded Resource"
3) add this in your AssemblyInfo.cs "[assembly: System.Web.UI.WebResource("namespace.directory.imageName.png", "img/png")]"
4) in your *.cs file add this to show the image: Image1.ImageUrl = Page.ClientScript.GetWebResourceUrl(typeof(_Default), "namespace.directory.imageName.png");
5) press f5, if there are no errors test the project using your webbrowser (localhost:yourPortNumber)
           

« Last Edit: February 11, 2009, 07:06:44 PM by HappyFace » Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
jurka
Staff
Member
*
*

Reputation: 5
Offline Offline
Posts: 188
Referrals: 0

Awards
« Reply #1 on: February 12, 2009, 11:50:17 AM »

Actually I can't understand this program use? Images are written inside DLL or ?
Logged
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« Reply #2 on: February 12, 2009, 02:05:16 PM »

Actually I can't understand this program use? Images are written inside DLL or ?

With this you can embed files (images, class files etc) directly into the dll to distribute or include in other projects without writing every class again.
« Last Edit: February 12, 2009, 02:13:52 PM by HappyFace » Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« Reply #3 on: February 14, 2009, 11:40:27 AM »

Updated example
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here