Create image from file programmatically asp.net C# - c#

I am writing project in asp.net C#. I want to create Image programmatically by the following code:
protected void Page_Load(object sender, EventArgs e)
{
Image image = new Image();
image.ID = "image11";
image.ImageUrl = "a.jpg";
image.ImageAlign = ImageAlign.AbsMiddle;
image.Visible = true;
}
But nothing is displayed when I run the project.
How to create image from file and display it in the page by writing code in .cs file?

You have created an image control but you have not added it to your form. Write the below code to add the image control to your form.
form1.Controls.Add(image);

At this point, you have just created an image, but you haven't added it to a control or page context to be displayed. You essentially said
int x = 10;
but then never did anything with x.
ASP.NET uses composition, so it maintains a collection of controls, with each control also containing a collection of children nodes. You need to add the image to a container. For instance, if you want to add the image to a panel named myPanel, it would be
myPanel.Controls.Add(image);
Check out this article.

You will need to create panel and then you will have to add that image to panel.
Panel Panel1= new Panel();
form1.Controls.Add(image);

Related

How to create tab pages content dynamically in windows forms?

I am using windows form to build an app that draws the form controls based on the connected device dynamically. So I have a tab control and when the user select tab3 for instance the tab page content will be drawing based on connected device for example add two text boxes and a button. How can I do this. I would like also to know how to position those controls after they are created.
private void tabPage3_Click(object sender, EventArgs e)
{
TextBox text = new TextBox();
this.tabPage3.Controls.Add(text);
}
As you just stated, you create your Controls like in your example. Positioning is achieved by the Left and Top Properties of your freshly created control. BUT, my advise is, it will be easier to use predefined UserControls and add them dynamically, because I think you don't have nearly unlimited types of devices.
If you are curious how Visual Studio Designer is creating those code, just look up Designer.cs in InitializeComponent()

How to reset an image to default in Windows Form

I have created a picture box in my Windows Form. I set it to a particular image in the Properties section. I then coded a method which changes the image depending on criteria. How would i reset the image to initial default image i had after it has been changed?
Doing something as simple as:
pbMyImage.Image = pbMyImage.InitialImage;
This will reset it to the value you set in the InitialImage in the properties window.
Have you tried simply changing it to the default image again inside your code?
pictureBoxName.BackgroundImage = yourDefaultImage;
Aditionally, create a new void method and call for it whenever you want to reset it to default:
public static void setImageToDefault()
{
pictureBoxName.BackgroundImage = yourDefaultImage;
}
Then call for the method setImageToDefault();
We can reset picture box to its initial image easily. You can also check this property set in resource file for required picture box. For example if name of our picture box is "picFacial" then we can reset it like
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmePassportApplication));
this.PicFacial.Image = ((System.Drawing.Image)(resources.GetObject("PicFacial.Image")));
this.PicFacial.InitialImage = ((System.Drawing.Image)(resources.GetObject("PicFacial.InitialImage")));
This is an old question, but hopefully this will help others stumbling upon the same issue ... you can put your particular image in the ErrorImage in section of properties window NOT in the image section .... then your code will be
private void btnDelImage_Click(object sender, EventArgs e)
{
pboxImage.Image = pboxImage.ErrorImage;
}

How to add image to ToolStripMenuItem

I have a C# winForm project that uses a ContextMenuStrip. I dynamically add ToolStripMenuItems to the ContextMenuStrip based on use interaction. When I add a new ToolStripMenuItem I set it's Text property and Image property. I don't know how to the set the Image property without getting the image from the location where it's at. How do I add the imagine to my project? Here's an example of what my code is doing
ContextMenuStrip cxtMnuStrp = new ContextMenuStrip;
private void Button_Click(object sender, EventArgs e)
{
// some filtering and logic
// to determine weather to
// create and add a ToolStripMenuItem
// blah, blah, blah...
ToolStripMenuItem item = new ToolStripMenuItem("uniqueName");
item.Image = Image.FromFile(#"C:\MyFolder\MyIcon.ico");
if (cxtMnuStrp.Items.ContainsKey(item) == false)
cxtMnuStrp.Items.Add(item);
}
With "item.Image = Image.FromFile(#"C:\MyFolder\MyIcon.ico")" When I distribute my each machine would have to have the "C:\MyFoler" directory and also have the "MyIcon.ico" on their computer in the "C:\MyFoler" directory.
Plus it doesn't seem right that I have hit the hard drive each time I want to add an icon to my ToolStripMenuItem
You could save your icons in a resource file or save the image as a embedded resource.
Using the resource file.
Adding and Editing Resources (Visual C#)
Adding the images as a embedded resource
Visual Studio: How to store an image resource as an Embedded Resource?
You code will will be as shown below.
private void BuildContextMenuStrip_Click(object sender, EventArgs e)
{
ContextMenuStrip cxtMnuStrp = new ContextMenuStrip();
ToolStripMenuItem item = new ToolStripMenuItem("uniqueName") { Image = WindowsFormsApplication2.Properties.Resources.Search.ToBitmap() };
if (cxtMnuStrp.Items.Contains(item) == false)
cxtMnuStrp.Items.Add(item);
this.ContextMenuStrip = cxtMnuStrp;
}
Note:
If you added a icon to the resource file. You will have to convert it to a image by using .ToBitmap().
The images are now available in intellisense instead of using path strings.
I have added the contextMenuStrip to the form in the example above.
In addition to the information provided on how to add resources in the links above you can add them as follows

Implementing activeX control to view PowerPoint slides in Windows forms using c#, not showing control

I'm working on a project and I need to embed a PowerPoint viewer in windows forms. I'm using the following activeX control: http://www.daolnwod.com/free-powerpoint-viewer-activex.html.
I activated the control to be used with the form designer's toolbox and dragged it into my form. I then edited the code in the InitializeComponent() method to the following:
this.axPowerPointViewer1 = new AxPOWERPOINTVIEWERLib.AxPowerPointViewer();
((System.ComponentModel.ISupportInitialize)(this.axPowerPointViewer1)).BeginInit();
this.axPowerPointViewer1.Enabled = true;
this.axPowerPointViewer1.Location = new System.Drawing.Point(0, 0);
this.axPowerPointViewer1.Name = "axPowerPointViewer1";
this.axPowerPointViewer1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axPowerPointViewer1.OcxState")));
this.axPowerPointViewer1.Size = new System.Drawing.Size(925, 573);
this.axPowerPointViewer1.TabIndex = 5;
//this.axPowerPointViewer1.CreateControl();
this.Controls.Add(this.axPowerPointViewer1);
((System.ComponentModel.ISupportInitialize)(this.axPowerPointViewer1)).EndInit();
And in my Forms constructor
public Form1()
{
InitializeComponent();
axPowerPointViewer1.Show();
bool loaded = axPowerPointViewer1.LoadFile(#"C:\Debug\test2.ppt"); // loaded = false
string z = axPowerPointViewer1.GetSlideCount().ToString();
}
However, when I'm opening the form nothing shows up. The code compiles but I can't see my test slide that I've been working on. I have created 2 buttons for 'Previous' and 'Next' slides but debugging gives me a slide location of 0 every time so something must be wrong and I can't seem to find it.
UPDATE
The problem has been solved. It seems I didn't call axPowerPointviewer1.InitControl(). It still has a few troubles, sometimes it won't display the first slide at startup. If things keep running smoothly I'll post an answer to this problem.
The problem is in initialising the control. In order for the control to fully function you need to call the InitControl() method so call calling the following code should make the program work:
private void Form1_Load(object sender, EventArgs e)
{
this.axPowerPointViewer1.InitControl();
}

I want to load default image in picturebox if the user does not select an image in C#

I am developing a window form in C#. In my window form there is a picture box. I want if the user does not select an image then the default image will be load in picture box which is save in my project folder.
thanks in advance
I guess you want to know how you can get the picture from the project folder right?
First add the picture to you project (add existing item) and set the Build Action to Embedded Resource:
then the following code do the trick:
private void SetPicture()
{
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
using (var imgStream = assembly.GetManifestResourceStream("DataGrid.TestImage.jpg"))
{
var img = new Bitmap(imgStream);
Picturebox.Image = img;
}
}
where "DataGrid" is MY project-name (you have to insert your own) and "TestImage.jpg" is the name of your Image (if you put it into a folder you might have to give the foldername too).
Picturebox is a PictureBox-Control I set the image to.
Either set an image in the designer and override it in code
for instance you can place this after user selection:
if (someConditionIndicatingUserAction)
pictureBoxControlName.Image = UserSelectedImage;
Or, you can store a default value in a variable and set the picturebox image to it (if the user selects nothing, you don't change the value, and if he does, change it accordingly)

Categories

Resources