How to reference the Paint.NET assemblies directly using C# - c#

I wanted to reference the Paint.NET assemblies directly and use a its functionality that way. i dont know how to use the .dll file PaintDotNet.Core.dll
and use it functionality in C# visual studio any helps. Please
want to reference to these assemblies: C:\Program Files\Paint.NET\PaintDotNet.*.dll Then poke around the classes in those namespaces.
Codes:-
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
string filename = "";
if (ofd.ShowDialog() == DialogResult.OK)
{
filename = System.IO.Path.GetFullPath(ofd.FileName);
}
// MessageBox.Show(filename, "file");
pictureBox1.ImageLocation = filename;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
DialogResult result = MessageBox.Show("Do you wish to continue?", "Save Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
System.Diagnostics.Process.Start(#"C:\Program Files\Paint.NET\PaintDotNet.exe");
// here i need to perform the function like
//Open + O`
//ctrl + Shift + L)` then `
//(ctrl + Shift + G)`. then save
//`ctrl + Shift + S`
}
else
{
return;
}
}

Just follow the instruction to send the shortcut key to another application
Add this namespace to the class
using System.Runtime.InteropServices;
Then declare SetForegroundWindow function with DllImport statement. this will create object of that method which has been created in User32.dll
[DllImport ("User32.dll")]
static extern int SetForegroundWindow(IntPtr point);
And add the following code to your button click or anywhere in your project. This code will navigate the OpenFileDialog to open the existing file in Paint.NET application.
private void button1_Click(object sender, EventArgs e)
{
Process p = Process.GetProcessesByName("PaintDotNet").FirstOrDefault();
if (p != null)
{
SetForegroundWindow(p.MainWindowHandle); //Set the Paint.NET application at front
SendKeys.SendWait("^(o)"); //^(o) will sends the Ctrl+O key to the application.
}
}
most of programmers made the mistake between Ctrl+O and Ctrl+o seems similar but, the ascii value of both key is different. So, make sure the key character is not in uppercase. You can also read the full information about SendKey method on msdn. You can make any key combination and send through the SendWait() method.

Just add one or some or all of the libraries to your project. as Measuring states then use the object explorer.
NOTE: never mind the .xaml stuff or the actual projects I am trying to render SharpDX D3D11 in a wpf app to make a map editor (and without the toolkit (don't ask me why. I am crazy)).
I swear I have the code last night are you trying to automate paint.net?
you will have to make a plug-in which would make the process way more streamlined than having to start a second app.

Related

MessageBox Features in WPF

I am trying to create a MessageBox that appears at the start of my program, asking the user if they would like to load a file or not. So far I have:
public static void LoadFile(object sender, FormClosingEventArgs e)
{
System.Windows.MessageBox.Show("Would you like to load a file?",
System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxQuestion);
if (result == DialogResult.No)
{
// cancel the closure of the form.
e.Cancel = true;
}
}
I realize that some of this code is used to exit out of the program. I don't intend to do this, it is just currently still left in from the sample code I was trying. When I try this code, I receive several errors, the major one involves the MessageBoxQuestion. The error reads
The type or namespace name 'MessageBoxQuestion' does not exist in the namespace System.Windows
I previously had this error on the MessageBoxButtons but fixed it by changing it to MessageBoxButton. Starting with just a simple message box, I originally had the code:
public static void LoadFile()
{
System.Windows.MessageBox.Show("Text");
}
This worked perfectly fine, despite the fact that I had to add the System.Windows. to remove the error
The name MessageBox does not exist in the current context.
Does anyone know a solution as to how I can get my MessageBox working correctly?
The WPF version of MessageBox differs from the Windows Forms' version. You need to use this overload.
Here is what I finally came up with:
public static void LoadFile()
{
// Configure message box
string message = "Would you like to load a file?";
string caption = "Startup";
System.Windows.MessageBoxButton buttons = System.Windows.MessageBoxButton.YesNo;
System.Windows.MessageBoxImage icon = System.Windows.MessageBoxImage.Information;
// Show message box
System.Windows.MessageBoxResult result =
System.Windows.MessageBox.Show(message, caption, buttons, icon);
if(result == System.Windows.MessageBoxResult.Yes)
{
}
else if(result == System.Windows.MessageBoxResult.No)
{
}
}
I included the if else branch that I planned to use later.

FIleDialog Handling with MVVM (Model-View-ViewModel) with WPF

I´m a total C# noob, so please be indulgent with me ;)
I´m just working on an WPF Application and want to implement my program in the MVVM-pattern.
I want to write a simple HTML editor.
So at first my application consists of a menu, which owns the item "File". There you should be able to select between "New", "Open", "Save", "Save As" and "Close".
During my first steps with C# I wrote these functions in the CodeBehind-File of the MainWindow and it worked. But now I want to implement my program in a "cleaner" way by using the MVVM pattern. But I really have problems to understand this pattern and especially to implement the SaveDialog and the OpenDialog without CodeBehind.
private void Oeffnen_Click(object sender, RoutedEventArgs e)
{
//neuen OpenDialog anlegen
webBrowser2 = new WebBrowser();
DockPanel.SetDock(webBrowser2, Dock.Top);
this.DockPanel1.Children.Add(webBrowser2);
opendlg = new Microsoft.Win32.OpenFileDialog();
opendlg.Filter = "html files (*.html)|*.html|htm files (*.htm)|*.htm";
//opendlg.RestoreDirectory = true;
//öffne Opendialog
if (opendlg.ShowDialog() == true)
{
try
{
if (opendlg.OpenFile() != null)
{
filename = opendlg.FileName;
webBrowser2.Navigate("file:///" + filename); //Öffne html Datei
doc2 = webBrowser2.Document as IHTMLDocument2;
doc2.designMode = "On"; //Editierbarkeit aktivieren
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
So this is for example my CodeBehind for the FileOpen-function.
Can someone maybe explain me on the example of the Open or SaveDialog or anyway else, how to use the MVVM pattern with this kind of application?
Sorry if this question is too general. Please ask for details.
Thanks!

How do I keep track of the last folder selected by a user?

I thought using application settings would do the trick but I'm not getting it to work. This is what I have:
private void btnBrowse_Click(object sender, EventArgs e)
{
if (fbFolderBrowser.ShowDialog() == DialogResult.OK)
{
// I want to open the last folder selected by the user here.
}
When the user clicks on this button, I want to open the browse window to the last folder he accessed and save it. Next time he clicks on the button, it'll automatically select that folder.
I was thinking maybe I could use user variables where I can change at run-time but I'm not getting it to work. Can anyone give me a hand?
Go to Settings Page, Project Designer of the project which you have created and add folder path variable inside the application. Now add below code to restore the last selected folder path.
FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
folderBrowser.Description = "Select a folder to extract to:";
folderBrowser.ShowNewFolderButton = true;
folderBrowser.SelectedPath = Properties.Settings.Default.Folder_Path;
//folderBrowser.SelectedPath = project_name.Properties.Settings.Default.Folder_Path;
if (folderBrowser.ShowDialog() == DialogResult.OK)
{
if (!String.IsNullOrEmpty(Properties.Settings.Default.Folder_Path))
Properties.Settings.Default.Folder_Path = folderBrowser.SelectedPath;
Properties.Settings.Default.Folder_Path = folderBrowser.SelectedPath;
Properties.Settings.Default.Save();
}
There are two places where you can find the last folder accessed by a user:
Recent Files and Folders: It can be found here: C:\Documents and Settings\USER\Recent
Registry: In the registry to look here: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU
You can use this snippet to find it:
public static string GetLastOpenSaveFile(string extention)
{
RegistryKey regKey = Registry.CurrentUser;
string lastUsedFolder = string.Empty;
regKey = regKey.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\OpenSaveMRU");
if (string.IsNullOrEmpty(extention))
extention = "html";
RegistryKey myKey = regKey.OpenSubKey(extention);
if (myKey == null && regKey.GetSubKeyNames().Length > 0)
myKey = regKey.OpenSubKey(regKey.GetSubKeyNames()[regKey.GetSubKeyNames().Length - 2]);
if (myKey != null)
{
string[] names = myKey.GetValueNames();
if (names != null && names.Length > 0)
{
lastUsedFolder = (string)myKey.GetValue(names[names.Length - 2]);
}
}
return lastUsedFolder;
}
OR
In windows XP when you press Save on a SaveFileDialog the directory where the file is saved, is set as the new current working directory (the one in Environment.CurrentDirectory).
In this way, when you reopen the FileDialog, it is opened on the same directory as before.
By setting FileDialog.RestoreDirectory = true, when you close the FileDialog the original working directory is restored.
In Windows Vista/Seven the behavior is always as FileDialog.RestoreDirectory = true.
Application settings can do the trick. A more elaborated version is here
use a Setting of type string
create a setting for each button and store the Path there. Then use
the setting as the ofd.InitialPath
using the above code example, try this:
right click your app name in Solution Explorer, click on the Settings
tab Name = Button1Path Type = String Scope = User
then use this:
private void btnBrowse_Click(object sender, EventArgs e)
{
fbFolderBrowser.InitialDirectory=this.Settings.Button1Path;
if (fbFolderBrowser.ShowDialog() == DialogResult.OK)
{
// I want to open the last folder selected by the user here.
this.Settings.Button1Path=fbFolderBrowser.SelectedPath
}
}
You can easily keep track of your last-selected folder, like this:
public String LastSelectedFolder;
private void btnBrowse_Click(object sender, EventArgs e)
{
fbFolderBrowser.InitialDirectory=this.Settings.Button1Path;
if (fbFolderBrowser.ShowDialog() == DialogResult.OK)
{
// Save Last selected folder.
LastSelectedFolder = fbFolderBrowser.SelectedPath;
}
}
I know this is a very old thread, but none of the answers point to the simplest way to re-open the file browser on user's last location.
simply define RestoreDirectory = true.
Check the example
var fd = new OpenFileDialog
{
Filter = #"All Files|*.*",
RestoreDirectory = true,
CheckFileExists = true
};
Class OpenFileDialog api reference
https://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.restoredirectory(v=vs.110).aspx
Unless I misunderstood the intention of the post, this is by far the simplest way to achieve it. However, if you do need to print this last location, then check the other

Implementing an OpenFileDialog with sound file preview

I would like to implement an open file dialog or file browser that additionally offers a "Preview" button to play the currently selected sound file (wave format in particular, other formats are not necessary for this application).
I could create my own form with various controls such as a treeview and listbox to show the folders and files, but I think I would be reinventing the wheel, or if nothing else going to a lot of work for something very simple. Do you recommend doing this?
Can I modify (inherit) the existing OpenFileDialog and add the sound-playing button to it somehow?
Is there some free library of custom file pickers that could be utilized? (Provided that the license allows inclusion in a commercial sense.)
Before you get carried away hacking the dialog, consider a simple solution first that leverages the FileOk event. Create a form named, say, frmPreview. Give it a constructor that takes a string. You'll need a Cancel and an OK button and code to play the file.
Display that form like this:
var dlg = new OpenFileDialog();
// Set other dlg properties...
dlg.FileOk += (s, cancel) => {
using (var prev = new frmPreview(dlg.FileName)) {
if (prev.ShowDialog() != DialogResult.OK) cancel.Cancel = true;
}
};
if (dlg.ShowDialog(this) == DialogResult.OK) {
// use the file
//...
}
Now, whenever the user clicks Open, your preview form shows up. The user can click Cancel and pick another file from the dialog.
Found this question whilst searching before asking my own. Possible slight simplification of Hans' answer is to use a standard Message Box rather than having to write your own form. Still a popup on a popup though.
private void btnSelect_Click(object sender, RoutedEventArgs e) {
var dlg = new Microsoft.Win32.OpenFileDialog {
DefaultExt = ".csv",
Filter = "Wav Files Only (*.wav)|*.wav",
InitialDirectory = "C:\\Windows\\Media\\",
CheckFileExists = true
};
dlg.FileName = "preselect the existing file if you wish";
dlg.FileOk += (s, cancel) => {
var player = new MediaPlayer();
player.Open(new Uri(dlg.FileName));
player.Play();
var msgres = MessageBox.Show(Path.GetFileName(dlg.FileName)+"\nUse this sound?", "Sound Playing", MessageBoxButton.YesNo);
if (msgres != MessageBoxResult.Yes) cancel.Cancel = true;
player.Stop(); //in case it is a long sound
};
var result = dlg.ShowDialog();
if (result != true) return;
//do whatever with dlg.FileName ...
}
Using a MessageBox provides a clean standard interface
Regarding point 2, I had thought the OpenFileDialog (or SaveFileDialog) weren't extendable in any way - they are provided by the OS.
But, it turns out they could be:
http://www.codeproject.com/KB/dialog/WPFCustomFileDialog.aspx
http://www.codeproject.com/KB/dialog/CustomizeFileDialog.aspx
The first one looks like what you're wanting to achieve.
Good luck.

how to burn the files selected from listview in a dvd using c#.net code?

can any expert help me out to solve a problem of burning a dvd using c#.net as a front end??
i need to select files from the listview in winform and then on button click i need to burn those multiple files in the dvd..
the concept is to select the multiple files from listview then on button click it should make a folder in some desired drive.. and then it should burn that complete folder in the dvd..this whole process should be performed during a single button click....
is there any way out??
the code should be compatible to use in .net2008 and windowsXP are the given codes compatible??
im using the componenet to get the dll/class lib. from (msdn.microsoft.com/en-au/vcsharp/aa336741.aspx) but its giving me error message "there are no components in d:\filepath\burncomponent.dll to be placed on the toolbox
private void button1_Click(object sender, EventArgs e)
{
XPBurnCD cd = new XPBurnCD();
cd.BurnComplete += new NotifyCompletionStatus(BurnComplete);
MessageBox.Show(cd.BurnerDrive);
DirectoryInfo dir = new DirectoryInfo(_burnFolder);
foreach (FileInfo file in dir.GetFiles())
{
cd.AddFile(file.FullName, file.Name);
}
cd.RecordDisc(false, false);
}
private void BurnComplete(uint status)
{
MessageBox.Show("Finished writing files to disc");
}
private void button2_Click_1(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.ShowNewFolderButton = false;
fbd.Description = "Please select a folder";
fbd.RootFolder = System.Environment.SpecialFolder.DesktopDirectory;
if (fbd.ShowDialog() == DialogResult.OK)
{
_burnFolder = fbd.SelectedPath;
}
else
{
_burnFolder = string.Empty;
}
}
Check out http://msdn.microsoft.com/en-au/vcsharp/aa336741.aspx
One simple approach could be to use the command line tools dvdburn and cdburn, which are belonging to XP. For example take a look at this site.
Update
Yes, it is a console application, but you can start it within a .Net Application by using the Process class. And here you should especially take a deeper look into the StartInfo property and its members, cause here you can set the parameters or redirect the output into your program to get informations about what the program is doing.

Categories