derekthornton
asked on
Open With ...
I've written a handy little Image Viewing application, but I'm confused as to how I can make it be opened from Windows. For instance, when the user double clicks an image file, it will open my program AND load the image file. I can understand how to make WINDOWS open it up, but how can I make it pass the Image clicked on through a constructor to load it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you need any help with this, please ask.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
But what if I want to have a constructor that loads the images 'IF' they opened it from an image? They might just open it normally, and in that case I don't want anything to be loaded.
Using my example above, in the Form_Load event you could add code like this:
if(filePath != null)
// Construct object
That way if they launch the program normally without double-clicking a file, filePath will be null and the program will execute normally.
if(filePath != null)
// Construct object
That way if they launch the program normally without double-clicking a file, filePath will be null and the program will execute normally.
ASKER
I've tried your code verbatim except I put the Image Loading method in the Load_Event and it doesn't work. Is there anything I may be missing?
Can you post your Form_Load event handler code?
ASKER
Will it work to test it by compiling a Debug Project and going to an image, right clicking and using "Open With ..." and chosing the debug .exe file of my program to do it?
ASKER
private void PictureViewer_Load(object sender, System.EventArgs e)
{
if(filePath != null)
OpenPictures(filePath);
}
private void OpenPictures(String File)
{
Images.ImagesList.Add(Imag e.FromFile (File));
pbPlus.Image = Images.CurrentImage;
ImageSizeMode = SizeMode.Isotropic;
Invalidate();
}
{
if(filePath != null)
OpenPictures(filePath);
}
private void OpenPictures(String File)
{
Images.ImagesList.Add(Imag
pbPlus.Image = Images.CurrentImage;
ImageSizeMode = SizeMode.Isotropic;
Invalidate();
}
The form load code looks good. Yes, you can test it with the way you described.
ASKER
Ok.Well. It still doesn't work.
What error are you getting?
ASKER
No error. Just ..nothing happens.
Is this when you double-click a file associated with it or when you launch the program?
ASKER
I go to a picture file, right click it, choose "Open With" find the Debug executable for my program, and select it, and the program opens up and nothing happens.
Did you add the mentioned code to the Main method? Also, as a debug measure, add this line as the first thing in your Form_Load event handler:
MessageBox.Show(filePath);
Try running the application normally and see what it says, then try opening a file and see what it says.
MessageBox.Show(filePath);
Try running the application normally and see what it says, then try opening a file and see what it says.
ASKER
Done both and it doesn't help.
ASKER
private void OpenPictures(String args)
{
Images.ImagesList.Add(Imag e.FromFile (args));
pbPlus.Image = Images.CurrentImage;
ImageSizeMode = SizeMode.Isotropic;
Invalidate();
}
private void PictureViewer_Load(object sender, System.EventArgs e)
{
if(filePath != null)
MessageBox.Show(filePath);
}
[STAThread]
public static void Main(string[] args)
{
if(args.Length > 0)
filePath = args[0];
Application.Run(new PictureViewer());
}
{
Images.ImagesList.Add(Imag
pbPlus.Image = Images.CurrentImage;
ImageSizeMode = SizeMode.Isotropic;
Invalidate();
}
private void PictureViewer_Load(object sender, System.EventArgs e)
{
if(filePath != null)
MessageBox.Show(filePath);
}
[STAThread]
public static void Main(string[] args)
{
if(args.Length > 0)
filePath = args[0];
Application.Run(new PictureViewer());
}
No, I mean put the MessageBox above the rest of the code.
private void PictureViewer_Load(object sender, System.EventArgs e)
{
MessageBox.Show(filePath);
}
private void PictureViewer_Load(object sender, System.EventArgs e)
{
MessageBox.Show(filePath);
}
ASKER
What do you mean?
Make the message box the first line of code in the Form_Load event handler. Do not enclose it in an if statement.
ASKER
I do, and still nothing happens.
You should at least be getting an empty message box.
ASKER
Not even an empty message box.
Sorry this is taking so long. Can you please paste your Form_Load again? Also make sure that you declared
private static string filePath; at the top of your class. I put together a dummy test program before I posted the original code and it all worked so I know this method works.
private static string filePath; at the top of your class. I put together a dummy test program before I posted the original code and it all worked so I know this method works.
ASKER
namespace PictureViewer
{
using System;
using System.Drawing;
using System.Windows.Forms;
using DevComponents.DotNetBar;
public enum SizeMode
{
Isotropic,
Scrollable
}
public sealed class PictureViewer : Form
{
#region -- Instance Fields --
private System.ComponentModel.ICon tainer components;
private ImageArray Images;
private PictureBoxPlus.PictureBoxP lus pbPlus;
private DevComponents.DotNetBar.Do ckSite barTopDockSite;
private DevComponents.DotNetBar.Do ckSite barBottomDockSite;
private DevComponents.DotNetBar.Do ckSite barLeftDockSite;
private DevComponents.DotNetBar.Do ckSite barRightDockSite;
private DevComponents.DotNetBar.Do tNetBarMan ager dnbMenu;
private System.Windows.Forms.Panel panel1;
private SizeMode sizeMode;
private static string filePath;
#endregion
#region -- Constructor and Destructor --
public PictureViewer()
{
// Required Windows Form Designer
InitializeComponent();
// Create the Images Array
Images = new ImageArray();
// ResizeRedraw
ResizeRedraw = true;
}
#endregion
#region -- Loading Point --
[STAThread]
public static void Main(string[] args)
{
if(args.Length > 0)
filePath = args[0];
Application.Run(new PictureViewer());
}
#endregion
#region -- Windows Form Designer --
private void InitializeComponent()
{
this.components = new System.ComponentModel.Cont ainer();
this.pbPlus = new PictureBoxPlus.PictureBoxP lus();
this.barTopDockSite = new DevComponents.DotNetBar.Do ckSite();
this.barBottomDockSite = new DevComponents.DotNetBar.Do ckSite();
this.barLeftDockSite = new DevComponents.DotNetBar.Do ckSite();
this.barRightDockSite = new DevComponents.DotNetBar.Do ckSite();
this.dnbMenu = new DevComponents.DotNetBar.Do tNetBarMan ager(this. components );
this.panel1 = new System.Windows.Forms.Panel ();
this.panel1.SuspendLayout( );
this.SuspendLayout();
//
// pbPlus
//
this.pbPlus.Isotropic = true;
this.pbPlus.Location = new System.Drawing.Point(8, 8);
this.pbPlus.Name = "pbPlus";
this.pbPlus.SizeMode = System.Windows.Forms.Pictu reBoxSizeM ode.Stretc hImage;
this.pbPlus.TabIndex = 6;
this.pbPlus.TabStop = false;
//
// barTopDockSite
//
this.barTopDockSite.Access ibleRole = System.Windows.Forms.Acces sibleRole. Window;
this.barTopDockSite.Backgr oundImageA lpha = ((System.Byte)(255));
this.barTopDockSite.Dock = System.Windows.Forms.DockS tyle.Top;
this.barTopDockSite.Locati on = new System.Drawing.Point(0, 0);
this.barTopDockSite.Name = "barTopDockSite";
this.barTopDockSite.Size = new System.Drawing.Size(496, 27);
this.barTopDockSite.TabInd ex = 2;
this.barTopDockSite.TabSto p = false;
//
// barBottomDockSite
//
this.barBottomDockSite.Acc essibleRol e = System.Windows.Forms.Acces sibleRole. Window;
this.barBottomDockSite.Bac kgroundIma geAlpha = ((System.Byte)(255));
this.barBottomDockSite.Doc k = System.Windows.Forms.DockS tyle.Botto m;
this.barBottomDockSite.Loc ation = new System.Drawing.Point(0, 366);
this.barBottomDockSite.Nam e = "barBottomDockSite";
this.barBottomDockSite.Siz e = new System.Drawing.Size(496, 0);
this.barBottomDockSite.Tab Index = 3;
this.barBottomDockSite.Tab Stop = false;
//
// barLeftDockSite
//
this.barLeftDockSite.Acces sibleRole = System.Windows.Forms.Acces sibleRole. Window;
this.barLeftDockSite.Backg roundImage Alpha = ((System.Byte)(255));
this.barLeftDockSite.Dock = System.Windows.Forms.DockS tyle.Left;
this.barLeftDockSite.Locat ion = new System.Drawing.Point(0, 27);
this.barLeftDockSite.Name = "barLeftDockSite";
this.barLeftDockSite.Size = new System.Drawing.Size(0, 339);
this.barLeftDockSite.TabIn dex = 0;
this.barLeftDockSite.TabSt op = false;
//
// barRightDockSite
//
this.barRightDockSite.Acce ssibleRole = System.Windows.Forms.Acces sibleRole. Window;
this.barRightDockSite.Back groundImag eAlpha = ((System.Byte)(255));
this.barRightDockSite.Dock = System.Windows.Forms.DockS tyle.Right ;
this.barRightDockSite.Loca tion = new System.Drawing.Point(496, 27);
this.barRightDockSite.Name = "barRightDockSite";
this.barRightDockSite.Size = new System.Drawing.Size(0, 339);
this.barRightDockSite.TabI ndex = 1;
this.barRightDockSite.TabS top = false;
//
// dnbMenu
//
this.dnbMenu.AllowUserBarC ustomize = false;
this.dnbMenu.AlwaysShowFul lMenus = true;
this.dnbMenu.AutoDispatchS hortcuts.A dd(DevComp onents.Dot NetBar.eSh ortcut.F1) ;
this.dnbMenu.AutoDispatchS hortcuts.A dd(DevComp onents.Dot NetBar.eSh ortcut.Ctr lC);
this.dnbMenu.AutoDispatchS hortcuts.A dd(DevComp onents.Dot NetBar.eSh ortcut.Ctr lA);
this.dnbMenu.AutoDispatchS hortcuts.A dd(DevComp onents.Dot NetBar.eSh ortcut.Ctr lV);
this.dnbMenu.AutoDispatchS hortcuts.A dd(DevComp onents.Dot NetBar.eSh ortcut.Ctr lX);
this.dnbMenu.AutoDispatchS hortcuts.A dd(DevComp onents.Dot NetBar.eSh ortcut.Ctr lZ);
this.dnbMenu.AutoDispatchS hortcuts.A dd(DevComp onents.Dot NetBar.eSh ortcut.Del );
this.dnbMenu.AutoDispatchS hortcuts.A dd(DevComp onents.Dot NetBar.eSh ortcut.Ins );
this.dnbMenu.BottomDockSit e = this.barBottomDockSite;
this.dnbMenu.DefinitionNam e = "PictureViewer.dnbMenu.xml ";
this.dnbMenu.LeftDockSite = this.barLeftDockSite;
this.dnbMenu.MdiSystemItem Visible = false;
this.dnbMenu.ParentForm = this;
this.dnbMenu.RightDockSite = this.barRightDockSite;
this.dnbMenu.Style = DevComponents.DotNetBar.eD otNetBarSt yle.Office 2003;
this.dnbMenu.TopDockSite = this.barTopDockSite;
this.dnbMenu.UseHook = true;
this.dnbMenu.ItemClick += new System.EventHandler(this.d nbMenu_Ite mClick);
//
// panel1
//
this.panel1.BackColor = System.Drawing.SystemColor s.ActiveCa ption;
this.panel1.BorderStyle = System.Windows.Forms.Borde rStyle.Fix edSingle;
this.panel1.Controls.Add(t his.pbPlus );
this.panel1.Dock = System.Windows.Forms.DockS tyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 27);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(496, 339);
this.panel1.TabIndex = 7;
//
// PictureViewer
//
this.AutoScale = false;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(496, 366);
this.Controls.Add(this.pan el1);
this.Controls.Add(this.bar LeftDockSi te);
this.Controls.Add(this.bar RightDockS ite);
this.Controls.Add(this.bar TopDockSit e);
this.Controls.Add(this.bar BottomDock Site);
this.Name = "PictureViewer";
this.WindowState = System.Windows.Forms.FormW indowState .Maximized ;
this.Load += new System.EventHandler(this.P ictureView er_Load);
this.panel1.ResumeLayout(f alse);
this.ResumeLayout(false);
}
#endregion
#region -- Menu --
private void dnbMenu_ItemClick(object sender, System.EventArgs e)
{
BaseItem mnuItemClicked = sender as BaseItem;
switch(mnuItemClicked.Name )
{
case "mnuOpenPicture":
OpenPicture();
break;
case "mnuNEXT":
NextPicture();
break;
case "mnuBACK":
LastPicture();
break;
case "mnuLAYOUT":
ToggleItem(mnuItemClicked) ;
SetSizeMode(mnuItemClicked );
break;
}
}
#endregion
#region -- Open Images --
private void OpenPicture()
{
OpenFileDialog OpenFile = new OpenFileDialog();
OpenFile.Filter = "All Images|*.*";
OpenFile.Multiselect = true;
if(OpenFile.ShowDialog() == DialogResult.OK)
{
foreach(string s in OpenFile.FileNames)
{
Images.ImagesList.Add(Imag e.FromFile (s));
}
pbPlus.Image = Images.CurrentImage;
ImageSizeMode = SizeMode.Isotropic;
Invalidate();
}
}
private void OpenPictures(String args)
{
Images.ImagesList.Add(Imag e.FromFile (args));
pbPlus.Image = Images.CurrentImage;
ImageSizeMode = SizeMode.Isotropic;
Invalidate();
}
#endregion
#region -- View Images --
private void NextPicture()
{
pbPlus.Image = Images.NextImage();
}
private void LastPicture()
{
pbPlus.Image = Images.PreviousImage();
}
#endregion
#region -- Image Viewing Methods --
private void ToggleItem(object sender)
{
// Retrieve the Button Clicked
ButtonItem btnObject = sender as ButtonItem;
// Swap it's status
btnObject.Checked = !btnObject.Checked;
if(btnObject.Checked)
btnObject.Text = "Isotropic ( Size to Fit )";
else if(!btnObject.Checked)
btnObject.Text = "Scrollable ( Full Size )";
}
private void SetSizeMode(object sender)
{
ButtonItem btnObject = sender as ButtonItem;
if(btnObject.Checked)
ImageSizeMode = SizeMode.Isotropic;
if(!btnObject.Checked)
ImageSizeMode = SizeMode.Scrollable;
}
#endregion
#region -- Layout Methods --
private void SetLayout()
{
switch(sizeMode)
{
case SizeMode.Isotropic:
pbPlus.Isotropic = true;
Invalidate();
break;
case SizeMode.Scrollable:
pbPlus.Isotropic = false;
Invalidate();
CenterImage();
break;
default:
pbPlus.Isotropic = true;
break;
}
}
private void CenterImage()
{
if(pbPlus.Image != null)
{
pbPlus.Location = new Point(0,0);
Int32 iTop = (Int32)((Height - pbPlus.Image.Height)/2.0);
Int32 iLeft = (Int32)((Width - pbPlus.Image.Width)/2.0);
switch(iTop.CompareTo(0))
{
case -1:
iTop = 0;
break;
}
switch(iLeft.CompareTo(0))
{
case -1:
iLeft = 0;
break;
}
pbPlus.Top = iTop;
pbPlus.Left = iLeft;
}
}
#endregion
#region -- Formatting Control --
private void IsotropicScale(Rectangle DrawRectangle)
{
panel1.AutoScroll = false;
SizeF sizeRatio =
new SizeF(
pbPlus.Image.Width / pbPlus.Image.HorizontalRes olution,
pbPlus.Image.Height / pbPlus.Image.VerticalResol ution
);
float IsotropicRatio =
Math.Min(DrawRectangle.Wid th / sizeRatio.Width,
DrawRectangle.Height / sizeRatio.Height);
sizeRatio.Width *= IsotropicRatio;
sizeRatio.Height *= IsotropicRatio;
pbPlus.Location = new Point(
(int)((DrawRectangle.X + (DrawRectangle.Width - sizeRatio.Width) /2)),
(int)(DrawRectangle.Y + (DrawRectangle.Height - sizeRatio.Height) / 2) );
pbPlus.Width = (int) sizeRatio.Width;
pbPlus.Height = (int) sizeRatio.Height;
}
private void ScrollableScale()
{
pbPlus.Height = pbPlus.Image.Height;
pbPlus.Width = pbPlus.Image.Width;
panel1.AutoScroll = true;
}
#endregion
#region -- Paint --
protected override void OnPaint(PaintEventArgs e)
{
if(pbPlus.Image != null)
{
if(pbPlus.Isotropic)
{
IsotropicScale(ClientRecta ngle);
}
else if (!pbPlus.Isotropic)
{
ScrollableScale();
}
}
base.OnPaint (e);
}
#endregion
private void PictureViewer_Load(object sender, System.EventArgs e)
{
//if(filePath != null)
MessageBox.Show(filePath);
}
#region -- Properties --
/// <summary>
/// Gets or sets the associated image
/// </summary>
public Image Image
{
get
{
return this.pbPlus.Image;
}
set
{
this.pbPlus.Image = value;
}
}
/// <summary>
/// Gets or Sets the Image Size Mode
/// </summary>
public SizeMode ImageSizeMode
{
get
{
return sizeMode;
}
set
{
sizeMode = value;
SetLayout();
}
}
#endregion
}
}
namespace PictureBoxPlus
{
using System;
using System.Drawing;
using System.Windows.Forms;
public sealed class PictureBoxPlus : PictureBox
{
#region -- Instance Fields --
/// <summary>
/// Isotropic Evaluator
/// </summary>
private Boolean blnIsotropic;
#endregion
#region -- Constructor and Destructor --
/// <summary>
/// Constructor
/// </summary>
public PictureBoxPlus() { }
/// <summary>
/// Destructor
/// </summary>
~PictureBoxPlus()
{
}
#endregion
#region -- Properties --
#region -- [Properties] Isotropic --
/// <summary>
/// Gets or Sets the Isotropic Property of the PictureBox
/// </summary>
public Boolean Isotropic
{
get
{
return blnIsotropic;
}
set
{
blnIsotropic = value;
Invalidate();
}
}
#endregion
#endregion
}
}
namespace PictureViewer
{
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
public sealed class ImageArray : Control
{
#region -- Instance Fields --
/// <summary>
/// The Images
/// </summary>
private ArrayList arrayListImages;
/// <summary>
/// Currently Selected Image
/// </summary>
private int CurrentIndex;
#endregion
#region -- Constructor, Destructor --
/// <summary>
/// Creates an array of images
/// </summary>
public ImageArray()
{
// Initialize The Images Array
this.arrayListImages = new ArrayList();
}
#endregion
#region -- Image Loading Methods --
#region -- [ Internal ] --
/// <summary>
/// Gets the next image in the list
/// </summary>
/// <returns>The Index of the next image</returns>
private int Next()
{
return Selected += 1;
}
/// <summary>
/// Gets the previous image in the list
/// </summary>
/// <returns>The index of the previous image</returns>
private int Previous()
{
return Selected -= 1;
}
#endregion
#region -- [ External ] --
/// <summary>
/// Retrieves the next image
/// </summary>
/// <returns>The Next Image, or the first image,
/// if the current image is the last</returns>
public Image NextImage()
{
if(Selected < ImagesList.Count - 1)
return (Image)ImagesList[Next()];
else
return (Image)ImagesList[Start()] ;
}
/// <summary>
/// Retrieves the previous image
/// </summary>
/// <returns>The Previous Image, o
/// r the last image, if the current image is the first</returns>
public Image PreviousImage()
{
if(Selected > 0)
return (Image)ImagesList[Previous ()];
else
return (Image)ImagesList[End()];
}
#endregion
#region -- [ Resets ] --
private int Start()
{
return (Selected = 0);
}
private int End()
{
return (Selected = ImagesList.Count - 1);
}
#endregion
#region -- [ Current Image ] --
public Image CurrentImage
{
get
{
return (Image)ImagesList[Selected ];
}
}
#endregion
#endregion
#region -- Properties --
/// <summary>
/// Gets or Sets the images in the array
/// </summary>
public ArrayList ImagesList
{
get
{
return this.arrayListImages;
}
set
{
this.arrayListImages = value;
}
}
/// <summary>
/// Gets or Sets the selected image
/// </summary>
private int Selected
{
get
{
return this.CurrentIndex;
}
set
{
this.CurrentIndex = value;
}
}
#endregion
}
}
{
using System;
using System.Drawing;
using System.Windows.Forms;
using DevComponents.DotNetBar;
public enum SizeMode
{
Isotropic,
Scrollable
}
public sealed class PictureViewer : Form
{
#region -- Instance Fields --
private System.ComponentModel.ICon
private ImageArray Images;
private PictureBoxPlus.PictureBoxP
private DevComponents.DotNetBar.Do
private DevComponents.DotNetBar.Do
private DevComponents.DotNetBar.Do
private DevComponents.DotNetBar.Do
private DevComponents.DotNetBar.Do
private System.Windows.Forms.Panel
private SizeMode sizeMode;
private static string filePath;
#endregion
#region -- Constructor and Destructor --
public PictureViewer()
{
// Required Windows Form Designer
InitializeComponent();
// Create the Images Array
Images = new ImageArray();
// ResizeRedraw
ResizeRedraw = true;
}
#endregion
#region -- Loading Point --
[STAThread]
public static void Main(string[] args)
{
if(args.Length > 0)
filePath = args[0];
Application.Run(new PictureViewer());
}
#endregion
#region -- Windows Form Designer --
private void InitializeComponent()
{
this.components = new System.ComponentModel.Cont
this.pbPlus = new PictureBoxPlus.PictureBoxP
this.barTopDockSite = new DevComponents.DotNetBar.Do
this.barBottomDockSite = new DevComponents.DotNetBar.Do
this.barLeftDockSite = new DevComponents.DotNetBar.Do
this.barRightDockSite = new DevComponents.DotNetBar.Do
this.dnbMenu = new DevComponents.DotNetBar.Do
this.panel1 = new System.Windows.Forms.Panel
this.panel1.SuspendLayout(
this.SuspendLayout();
//
// pbPlus
//
this.pbPlus.Isotropic = true;
this.pbPlus.Location = new System.Drawing.Point(8, 8);
this.pbPlus.Name = "pbPlus";
this.pbPlus.SizeMode = System.Windows.Forms.Pictu
this.pbPlus.TabIndex = 6;
this.pbPlus.TabStop = false;
//
// barTopDockSite
//
this.barTopDockSite.Access
this.barTopDockSite.Backgr
this.barTopDockSite.Dock = System.Windows.Forms.DockS
this.barTopDockSite.Locati
this.barTopDockSite.Name = "barTopDockSite";
this.barTopDockSite.Size = new System.Drawing.Size(496, 27);
this.barTopDockSite.TabInd
this.barTopDockSite.TabSto
//
// barBottomDockSite
//
this.barBottomDockSite.Acc
this.barBottomDockSite.Bac
this.barBottomDockSite.Doc
this.barBottomDockSite.Loc
this.barBottomDockSite.Nam
this.barBottomDockSite.Siz
this.barBottomDockSite.Tab
this.barBottomDockSite.Tab
//
// barLeftDockSite
//
this.barLeftDockSite.Acces
this.barLeftDockSite.Backg
this.barLeftDockSite.Dock = System.Windows.Forms.DockS
this.barLeftDockSite.Locat
this.barLeftDockSite.Name = "barLeftDockSite";
this.barLeftDockSite.Size = new System.Drawing.Size(0, 339);
this.barLeftDockSite.TabIn
this.barLeftDockSite.TabSt
//
// barRightDockSite
//
this.barRightDockSite.Acce
this.barRightDockSite.Back
this.barRightDockSite.Dock
this.barRightDockSite.Loca
this.barRightDockSite.Name
this.barRightDockSite.Size
this.barRightDockSite.TabI
this.barRightDockSite.TabS
//
// dnbMenu
//
this.dnbMenu.AllowUserBarC
this.dnbMenu.AlwaysShowFul
this.dnbMenu.AutoDispatchS
this.dnbMenu.AutoDispatchS
this.dnbMenu.AutoDispatchS
this.dnbMenu.AutoDispatchS
this.dnbMenu.AutoDispatchS
this.dnbMenu.AutoDispatchS
this.dnbMenu.AutoDispatchS
this.dnbMenu.AutoDispatchS
this.dnbMenu.BottomDockSit
this.dnbMenu.DefinitionNam
this.dnbMenu.LeftDockSite = this.barLeftDockSite;
this.dnbMenu.MdiSystemItem
this.dnbMenu.ParentForm = this;
this.dnbMenu.RightDockSite
this.dnbMenu.Style = DevComponents.DotNetBar.eD
this.dnbMenu.TopDockSite = this.barTopDockSite;
this.dnbMenu.UseHook = true;
this.dnbMenu.ItemClick += new System.EventHandler(this.d
//
// panel1
//
this.panel1.BackColor = System.Drawing.SystemColor
this.panel1.BorderStyle = System.Windows.Forms.Borde
this.panel1.Controls.Add(t
this.panel1.Dock = System.Windows.Forms.DockS
this.panel1.Location = new System.Drawing.Point(0, 27);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(496, 339);
this.panel1.TabIndex = 7;
//
// PictureViewer
//
this.AutoScale = false;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(496, 366);
this.Controls.Add(this.pan
this.Controls.Add(this.bar
this.Controls.Add(this.bar
this.Controls.Add(this.bar
this.Controls.Add(this.bar
this.Name = "PictureViewer";
this.WindowState = System.Windows.Forms.FormW
this.Load += new System.EventHandler(this.P
this.panel1.ResumeLayout(f
this.ResumeLayout(false);
}
#endregion
#region -- Menu --
private void dnbMenu_ItemClick(object sender, System.EventArgs e)
{
BaseItem mnuItemClicked = sender as BaseItem;
switch(mnuItemClicked.Name
{
case "mnuOpenPicture":
OpenPicture();
break;
case "mnuNEXT":
NextPicture();
break;
case "mnuBACK":
LastPicture();
break;
case "mnuLAYOUT":
ToggleItem(mnuItemClicked)
SetSizeMode(mnuItemClicked
break;
}
}
#endregion
#region -- Open Images --
private void OpenPicture()
{
OpenFileDialog OpenFile = new OpenFileDialog();
OpenFile.Filter = "All Images|*.*";
OpenFile.Multiselect = true;
if(OpenFile.ShowDialog() == DialogResult.OK)
{
foreach(string s in OpenFile.FileNames)
{
Images.ImagesList.Add(Imag
}
pbPlus.Image = Images.CurrentImage;
ImageSizeMode = SizeMode.Isotropic;
Invalidate();
}
}
private void OpenPictures(String args)
{
Images.ImagesList.Add(Imag
pbPlus.Image = Images.CurrentImage;
ImageSizeMode = SizeMode.Isotropic;
Invalidate();
}
#endregion
#region -- View Images --
private void NextPicture()
{
pbPlus.Image = Images.NextImage();
}
private void LastPicture()
{
pbPlus.Image = Images.PreviousImage();
}
#endregion
#region -- Image Viewing Methods --
private void ToggleItem(object sender)
{
// Retrieve the Button Clicked
ButtonItem btnObject = sender as ButtonItem;
// Swap it's status
btnObject.Checked = !btnObject.Checked;
if(btnObject.Checked)
btnObject.Text = "Isotropic ( Size to Fit )";
else if(!btnObject.Checked)
btnObject.Text = "Scrollable ( Full Size )";
}
private void SetSizeMode(object sender)
{
ButtonItem btnObject = sender as ButtonItem;
if(btnObject.Checked)
ImageSizeMode = SizeMode.Isotropic;
if(!btnObject.Checked)
ImageSizeMode = SizeMode.Scrollable;
}
#endregion
#region -- Layout Methods --
private void SetLayout()
{
switch(sizeMode)
{
case SizeMode.Isotropic:
pbPlus.Isotropic = true;
Invalidate();
break;
case SizeMode.Scrollable:
pbPlus.Isotropic = false;
Invalidate();
CenterImage();
break;
default:
pbPlus.Isotropic = true;
break;
}
}
private void CenterImage()
{
if(pbPlus.Image != null)
{
pbPlus.Location = new Point(0,0);
Int32 iTop = (Int32)((Height - pbPlus.Image.Height)/2.0);
Int32 iLeft = (Int32)((Width - pbPlus.Image.Width)/2.0);
switch(iTop.CompareTo(0))
{
case -1:
iTop = 0;
break;
}
switch(iLeft.CompareTo(0))
{
case -1:
iLeft = 0;
break;
}
pbPlus.Top = iTop;
pbPlus.Left = iLeft;
}
}
#endregion
#region -- Formatting Control --
private void IsotropicScale(Rectangle DrawRectangle)
{
panel1.AutoScroll = false;
SizeF sizeRatio =
new SizeF(
pbPlus.Image.Width / pbPlus.Image.HorizontalRes
pbPlus.Image.Height / pbPlus.Image.VerticalResol
);
float IsotropicRatio =
Math.Min(DrawRectangle.Wid
DrawRectangle.Height / sizeRatio.Height);
sizeRatio.Width *= IsotropicRatio;
sizeRatio.Height *= IsotropicRatio;
pbPlus.Location = new Point(
(int)((DrawRectangle.X + (DrawRectangle.Width - sizeRatio.Width) /2)),
(int)(DrawRectangle.Y + (DrawRectangle.Height - sizeRatio.Height) / 2) );
pbPlus.Width = (int) sizeRatio.Width;
pbPlus.Height = (int) sizeRatio.Height;
}
private void ScrollableScale()
{
pbPlus.Height = pbPlus.Image.Height;
pbPlus.Width = pbPlus.Image.Width;
panel1.AutoScroll = true;
}
#endregion
#region -- Paint --
protected override void OnPaint(PaintEventArgs e)
{
if(pbPlus.Image != null)
{
if(pbPlus.Isotropic)
{
IsotropicScale(ClientRecta
}
else if (!pbPlus.Isotropic)
{
ScrollableScale();
}
}
base.OnPaint (e);
}
#endregion
private void PictureViewer_Load(object sender, System.EventArgs e)
{
//if(filePath != null)
MessageBox.Show(filePath);
}
#region -- Properties --
/// <summary>
/// Gets or sets the associated image
/// </summary>
public Image Image
{
get
{
return this.pbPlus.Image;
}
set
{
this.pbPlus.Image = value;
}
}
/// <summary>
/// Gets or Sets the Image Size Mode
/// </summary>
public SizeMode ImageSizeMode
{
get
{
return sizeMode;
}
set
{
sizeMode = value;
SetLayout();
}
}
#endregion
}
}
namespace PictureBoxPlus
{
using System;
using System.Drawing;
using System.Windows.Forms;
public sealed class PictureBoxPlus : PictureBox
{
#region -- Instance Fields --
/// <summary>
/// Isotropic Evaluator
/// </summary>
private Boolean blnIsotropic;
#endregion
#region -- Constructor and Destructor --
/// <summary>
/// Constructor
/// </summary>
public PictureBoxPlus() { }
/// <summary>
/// Destructor
/// </summary>
~PictureBoxPlus()
{
}
#endregion
#region -- Properties --
#region -- [Properties] Isotropic --
/// <summary>
/// Gets or Sets the Isotropic Property of the PictureBox
/// </summary>
public Boolean Isotropic
{
get
{
return blnIsotropic;
}
set
{
blnIsotropic = value;
Invalidate();
}
}
#endregion
#endregion
}
}
namespace PictureViewer
{
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
public sealed class ImageArray : Control
{
#region -- Instance Fields --
/// <summary>
/// The Images
/// </summary>
private ArrayList arrayListImages;
/// <summary>
/// Currently Selected Image
/// </summary>
private int CurrentIndex;
#endregion
#region -- Constructor, Destructor --
/// <summary>
/// Creates an array of images
/// </summary>
public ImageArray()
{
// Initialize The Images Array
this.arrayListImages = new ArrayList();
}
#endregion
#region -- Image Loading Methods --
#region -- [ Internal ] --
/// <summary>
/// Gets the next image in the list
/// </summary>
/// <returns>The Index of the next image</returns>
private int Next()
{
return Selected += 1;
}
/// <summary>
/// Gets the previous image in the list
/// </summary>
/// <returns>The index of the previous image</returns>
private int Previous()
{
return Selected -= 1;
}
#endregion
#region -- [ External ] --
/// <summary>
/// Retrieves the next image
/// </summary>
/// <returns>The Next Image, or the first image,
/// if the current image is the last</returns>
public Image NextImage()
{
if(Selected < ImagesList.Count - 1)
return (Image)ImagesList[Next()];
else
return (Image)ImagesList[Start()]
}
/// <summary>
/// Retrieves the previous image
/// </summary>
/// <returns>The Previous Image, o
/// r the last image, if the current image is the first</returns>
public Image PreviousImage()
{
if(Selected > 0)
return (Image)ImagesList[Previous
else
return (Image)ImagesList[End()];
}
#endregion
#region -- [ Resets ] --
private int Start()
{
return (Selected = 0);
}
private int End()
{
return (Selected = ImagesList.Count - 1);
}
#endregion
#region -- [ Current Image ] --
public Image CurrentImage
{
get
{
return (Image)ImagesList[Selected
}
}
#endregion
#endregion
#region -- Properties --
/// <summary>
/// Gets or Sets the images in the array
/// </summary>
public ArrayList ImagesList
{
get
{
return this.arrayListImages;
}
set
{
this.arrayListImages = value;
}
}
/// <summary>
/// Gets or Sets the selected image
/// </summary>
private int Selected
{
get
{
return this.CurrentIndex;
}
set
{
this.CurrentIndex = value;
}
}
#endregion
}
}
Unfortunately I cannot run this code because I don't have the DotNetBar. In your Form_Load add this and comment out everything else in the event handler:
MessageBox.Show("Form load");
If you don't get a message here, then your Form_Load event handler isn't working. Also, which file format are you try to open with your program?
MessageBox.Show("Form load");
If you don't get a message here, then your Form_Load event handler isn't working. Also, which file format are you try to open with your program?
ASKER
JPEG
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks for the confirmation, eternal_21. That's what I expected to happen. In this case, you can replace the MessageBox with:
if(filePath != null)
OpenPictures(filePath);
if(filePath != null)
OpenPictures(filePath);
ASKER
It works if I compile, build the install, install it, and then run it ...but not if i just use the Debug or Release.exe. . . Strange..
ok.. so would it be plausible to make it work for a group of selected pictures?
ok.. so would it be plausible to make it work for a group of selected pictures?
When I open multiple files it opens multiple applications and I'm not really sure how to open them all in one window. Sorry.
Here is how I would do it, although I have no idea what common practices are!
First, I would create a method of detecting if an instance of my application is already running. You could use something like Michael Potter's Single Process Instance object http://www.codeproject.com/csharp/cssingprocess.asp). If you are starting a second instance of the program with no parameters, I would bring the first instance to the foreground. The website previously mentioned has information to help you do this.
Here comes the tricky part. If you are starting a second instance of the program with a filename, I would send a (custom) Windows message to the first application asking it to open the file (using user32.dll's SendMessage function - Single Process Instance object will be able to give you the main window handle of the first application instance). If the application is opened, and it is the first (and only) instance, it would implement a custom message listener (using a custom System.Windows.Forms.IMess ageFilter object), that waits for new instances of the application to request a file be opened.
Hope that made sense - If it doesn't let me know... And good work Timbo87 - It's good to see an expert putting so much effort into helping someone find a solution!
First, I would create a method of detecting if an instance of my application is already running. You could use something like Michael Potter's Single Process Instance object http://www.codeproject.com/csharp/cssingprocess.asp). If you are starting a second instance of the program with no parameters, I would bring the first instance to the foreground. The website previously mentioned has information to help you do this.
Here comes the tricky part. If you are starting a second instance of the program with a filename, I would send a (custom) Windows message to the first application asking it to open the file (using user32.dll's SendMessage function - Single Process Instance object will be able to give you the main window handle of the first application instance). If the application is opened, and it is the first (and only) instance, it would implement a custom message listener (using a custom System.Windows.Forms.IMess
Hope that made sense - If it doesn't let me know... And good work Timbo87 - It's good to see an expert putting so much effort into helping someone find a solution!
ASKER
I found a viable solution.
Using the System.IO.FileInfo class, I can get the directory of the image selected.
From there, I can use the "GetFiles()" method of the FileInfo Class to get an Array of the files in the present directory, and check their file extensions in a loop. If it ends with a '.jpg' or a '.bmp' or any other image type I want to incorporate, I add it to the Image Array.
Using the System.IO.FileInfo class, I can get the directory of the image selected.
From there, I can use the "GetFiles()" method of the FileInfo Class to get an Array of the files in the present directory, and check their file extensions in a loop. If it ends with a '.jpg' or a '.bmp' or any other image type I want to incorporate, I add it to the Image Array.
Hey derek, check this out: http://www.codeproject.com/cs/miscctrl/ddelistener.asp.