Unable to load images in Visual Studio 2015 - c#

I am having an issue with using the ImageLocation of a pictureBox. I went to:
Documents\VisualStudios\Projects\Program Name:
Then I create a new folder called images and within that folder i put the pictures(rock.png , paper.png )I intend to use. Keep in mind I can not load from the C drive, I have to turn this project in, so that it can work on any computer. Am I loading my images in the wrong location? or Am i accessing them wrong?
if (PlayerOne == Rock && PlayerTwo == Scissors)
{
ScoreOne++;
picture1.ImageLocation = #"images\rock.png";
picture2.ImageLocation = #"images\paper.png";
lblOneScore.Text = Convert.ToString(ScoreOne);
lblShowWinner.Text = " Player One Wins! ";
picture1.Load();
picture2.Load();
}

Instead of setting the ImageLocation property, try:
picture1.Load(#"C:\temp\pic.jpg");
The picturebox load method also accepts URL's (if you can't access drives and it needs to work on any PC without hardcoded UNC paths):
picture1.Load("http://i.stack.imgur.com/FmIGn.png");
Or use the app's location if you have packaged it with an image:
picture1.Load(Application.StartupPath + "\\a.jpg");
Or use a Resource file:
I would love to use resourceFile but I can not find it on Visual Studios 2015 only for 2013 and previous versions
It hasn't changed from VS2013 to VS2015, in your Winforms project, expand the Project Properties and double click on Resources.resx, then add an image:
picture1.Image = global::ProjectName.Properties.Resources.ImageName;

VisualStudios/Projects/fileName/filename/bin/Debug/create images folder and insert pictures in that folder and my original code posted works just fine

Related

Search and return path of given folder name (Windows and Linux)

I want to get the path of an existing folder SeleniumTestData inside the solution.
Why? My selenium tests should create at start of the test, temporary folder which are being ignored in Git, so each of my colleagues has their own TestData folders for their own TestExecutions on their machine (Like Save/Load Cookies) and dont pull TestData from other colleagues.
The folder where i want to create other folder by code is named SeleniumTestData folder and is inside:
..\source\repos\CoffeeTalk\src\Tests
I cant hardcore the path, as i'm facing here 2 problems:
Tests are being ran in Windows and Docker (Linux)
Co-Workers are saving the solution in different windows directories
Now i need a general solution which will work in any of these cases.
I already tried: var currentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
which returned: D:\source\repos\CoffeeTalk\src\Tests\Web\CoffeeTalk.Client.Selenium.Tests\bin\Debug\net6.0
and then tried to navigate back by executing the codeline currentDirectory?.Parent about 5-6 times. But its then again different in Linux.
Now im looking for a clean way. I suppose the first way i did it was not wrong by getting the CurrentDirectory and navigate back.
I already searched for solutions using stackoverflow, google. Either the solutions are outdated or im not getting the result im expecting.
Here i have the method which creates the folder, but im struggling with the GetFolderPath method.
public static void CreateFolder(string folderName, string newFolderName)
{
var folderPath = GetFolderPath(folderName);
var pathCombined = Path.Combine(folderPath, newFolderName);
var folderExists = Directory.Exists(pathCombined);
if (folderExists) return;
Directory.CreateDirectory(pathCombined);
}
Directory.GetCurrentDirectory isn't the directory with your executable file. It's something else (I don't know) that by the way depends on the OS. You should use this instead:
using System.Reflection;
// ...
string exeDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
And then go up the folders hierarchy as you want:
string neededFolder = new DirectoryInfo(exeDirectory).Parent.Parent.Parent.ToString(); // Or more "Parent" calls
As far as I know, it works on different OSs.

Trying Upload same file in windows form

I am working to develop a Windows form Project, we should use a Product picture.
Every this working well, we use File.Exists() to check if the file already exists then copy the image to the product Image folder.
if (image.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(image.FileName))
{
_view.FirmLogo.Image = new Bitmap(image.FileName);
_view.LogoURL = Path.Combine(System.IO.Path.GetFullPath(#"..\..\"), #"Resources\ProductImage\", Path.GetFileName(image.FileName));
//
if (File.Exists(_view.LogoURL))
{
File.Delete(_view.LogoURL);
}
//File.Move(#"c:\test\SomeFile.txt", #"c:\test\Test\SomeFile.txt");
File.Copy(image.FileName, _view.LogoURL);
}
The Bug
when we try to update the same file it fired an error:
"because it is being used by another process.
Ex. if we upload pic1, everything is well, we try to change to pic2 great job.
but if we try to back the pic1 fired the error especially if it happens in the same seesion.

Visual Studio Command for going to a specific item in source control explorer

Im looking for a way to automatically open Source Control Explorer from inside a plugincode.
So far I managed to open it by executing the command
View.TfsSourceControlExplorer
However, this does not seem to accept any arguments.
My goal here is to do something like this:
destination = "$/dev/framework/someFolder";
_dteObject.ExecuteCommand("View.TfsSourceControlExplorer", destination);
Which will them show me Source Control Explorer in the specified destination.
To anwser CSharpie's comment :
Also there seems to be a bug, if you call navigate to a file of the same directory as the explorer currently is in, everything will disappear.
I had the same problem, got two ways of solving this :
Truncate the filename from the path to only Navigate to folders.
Navigate to root first ("$/"), then navigate to the file you want.
Both works fine in VS2013.
And thanks for the "Application.DoEvent()" fix when the SourceControlExplorer's not opened.
Use the following code to show Source Control Explorer in the specified destination:
public void SelectFolder(string path)
{
dte.ExecuteCommand("View.TfsSourceControlExplorer");
Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExplorerExt explorer =
GetSourceControlExplorer();
if (explorer != null)
explorer.Navigate(path);
}
private Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExplorerExt GetSourceControlExplorer()
{
Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExt versionControl =
dte.GetObject("Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExt") as
Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExt;
if (versionControl == null)
return null;
return versionControl.Explorer;
}
I believe this is not possible. The source explorer detects the team project and drops you at the root of the team project node. $/myproject/ ..
Happy to be proven wrong on this one...

How to open a .DWG file in Autocad 2014 through C#

Hi I have a requirement in which I have to open a drawing file stored in C:\Temp Folder.
I tried the following code
public void launchacad(string pth) //pth is the path to the .DWG file
{
const string progID = "AutoCAD.Application.19.1";
const string exePath = #"C:\Program Files\Autodesk\AutoCAD 2014\acad.exe";
AcadApplication acApp = null;
try
{
acApp =
(AcadApplication)Marshal.GetActiveObject(progID);
}
catch { }
if (acApp != null)
{
MessageBox.Show("An instance of AutoCAD is already running.");
}
else
{
try
{
ProcessStartInfo psi =new ProcessStartInfo(exePath);
psi.WorkingDirectory = #"C:\Temp";
psi.Arguments = pth;
Process pr = Process.Start(psi);
pr.WaitForInputIdle();
while (acApp == null)
{
try
{
acApp =(AcadApplication)Marshal.GetActiveObject(progID);
}
catch
{
Application.DoEvents();
}
}
}
catch (Exception ex)
{
MessageBox.Show(
"Cannot create or attach to AutoCAD object: "
+ ex.Message
);
}
}
if (acApp != null)
{
acApp.Visible = true;
acApp.ActiveDocument.SendCommand("_MYCOMMAND ");
}
}
But as soon as Autocad starts it popups an error message saying Cannot find the specified drawing. When I use CMD.exe and type
"C:\Program Files\Autodesk\AutoCAD 2014\acad.exe" "C:\Temp\41 Stabd.dwg" It opens Autocad with the file(41 Stand.dwg) open.
I can't understand where I am making an error. Can someone help me out.
If still the drawing persists with problems, continue on to the next set of steps.
These can be done in any order, but have been listed in the order that Autodesk recommends makes the most sense. The file can be checked after each step. If things appear back to normal, there is no need to continue on to the rest of the steps.
​Open a blank DWG and type RECOVER at the command line. Browse to the problematic file to - allow AutoCAD a chance to restore the file.
Type OVERKILL at the command line, and select all objects. Check or uncheck properties to include or ignore, then click OK.
Type DELCON at the command line, and select all objects.
Type BREP and select all objects (if there are solids or surfaces in the file)
Type -SCALELISTEDIT, then "R" for reset, then "Y" for yes.
Type FILTERS, then click on the 'delete filters' button.
The DGNPURGE tool can be run if the file size is unexpectedly very large: http://knowledge.autodesk.com/article/AutoCAD-DWG-files-unexpectedly-increase-in-file-size
Try using a different version of AutoCAD to open the drawing, such as AutoCAD 2013 vs. AutoCAD 2015 or plain AutoCAD vs. AutoCAD Architecture, etc. Try different computers if available.
Open a blank DWG, and try to attach the problematic file as an XREF. If it allows you to attach the file, try next to BIND it to the current file. If that works, run the repair steps listed above.
Use the SAVEAS command to save the DWG in an older file format. Attempt to open the newly created file.
Export the file to DXF format using the DXFOUT command. Next, open a blan DWG and use the DXFIN command to import the file just created.
< Restore the Layout tabs:
Right-Click one of the default layout tabs
Select 'From Template...'
Open the original file
Choose the layout tabs to restore. (It is recommended to do this one tab at a time, in case one or more layout tabs are corrupted)
Move drawing objects between model and paper space. You may find that only one drawing space is usable in your file, although your main concern is model space:
​​1. Create a new layout and if need be, create a viewport.
Use CHSPACE to move all the geometry to paper space.
Create a new drawing and use the Design Center (ADC) to move the layout from the damaged file into it.
Use CHSPACE again to move the geometry back to model space.
Restore the original layouts from the bad file using the Design Center.
Dissect the drawing. In a copy of the file, conduct a process of elimination using QSELECT to select different object types and then delete them to see if that fixes what is wrong in the file. Do PURGE All after each deletion. Eventually you should remove the problem elements and then you can choose to leave them out, copy them in again from another file, recreate them, or further troubleshoot individual items to pinpoint exactly which one is problematic. A quick start to this whole process is to delete everything in the drawing and then test it. This will quickly tell you if the issue is with a drawing object or if it is a part of the drawing database.​

Setup project in c# windows application cant load image?

![enter image description here][1]i have a windows application in C# with a setup project !!!
I used Image.FromFile("filename") in my application but when make a setup project from it , and run it , it dose not show my pictures ! why ?
try
{
string timeOfDay = Convert.ToInt32(DateTime.Now.TimeOfDay.ToString().Substring(3, 2)).ToString();
this.BackgroundImage = Image.FromFile("BackGround\\Flowers (" + timeOfDay + ").jpg");
}
catch (Exception ex)
{
this.BackgroundImage = Image.FromFile("\\BackGround\\Flowers (59).jpg");
}
First you have to add the Image file to your Visual Studio project unless you do this image can not be embedded into the output assembly.
Project Folder > Images
First add a custom folder and move your image to that folder and add the same folder to your VS project. now go to Image property and set the Build Action as "Resource".

Categories