Click here to Skip to main content
15,901,035 members
Home / Discussions / C#
   

C#

 
AnswerRe: capture the screenshot of desktop using C# Pin
Afzaal Ahmad Zeeshan30-Jul-19 5:29
professionalAfzaal Ahmad Zeeshan30-Jul-19 5:29 
QuestionCreating a struct dynamically Pin
Member 1452625729-Jul-19 21:48
Member 1452625729-Jul-19 21:48 
AnswerRe: Creating a struct dynamically Pin
OriginalGriff29-Jul-19 22:06
mveOriginalGriff29-Jul-19 22:06 
AnswerRe: Creating a struct dynamically Pin
BillWoodruff31-Jul-19 14:28
professionalBillWoodruff31-Jul-19 14:28 
SuggestionRe: Creating a struct dynamically Pin
Richard Deeming1-Aug-19 1:40
mveRichard Deeming1-Aug-19 1:40 
GeneralRe: Creating a struct dynamically Pin
BillWoodruff1-Aug-19 8:47
professionalBillWoodruff1-Aug-19 8:47 
QuestionError when i try to add my control to form1 Pin
_Q12_29-Jul-19 8:30
_Q12_29-Jul-19 8:30 
AnswerRe: Error when i try to add my control to form1 Pin
Richard Deeming29-Jul-19 9:10
mveRichard Deeming29-Jul-19 9:10 
The problem is that when the code runs in the design view, the current directory is the Visual Studio application directory, not your project's directory. The file C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\google.png doesn't exist, so the Bitmap constructor throws an exception.

You can use the DesignMode property[^] to test whether your control is in design mode. You should probably also check that the file hasn't been deleted:
C#
Image img;

public tab()
{
    InitializeComponent();
    
    if (!DesignMode && File.Exists("google.png"))
    {
        img = new Bitmap("google.png");
    }
}

private void tab_Paint(object sender, PaintEventArgs e)
{
    if (img != null)
    {
        e.Graphics.DrawImage(img, rect);
    }
}
You should be able to adapt the code from this SO answer[^] to resolve the image path at design time, if you need to see the image in the designer:
C#
private string ResolveImagePath()
{
    string path = "google.png";
    if (DesignMode)
    {
        var typeResolver = (ITypeResolutionService)GetService(typeof(ITypeResolutionService));
        if (typeResolver != null)
        {
            AssemblyName name = System.Reflection.Assembly.GetExecutingAssembly().GetName();
            string basePath = typeResolver.GetPathOfAssembly(name);
            path = Path.Combine(basePath, path);
        }
    }
    
    return path;
}

public tab()
{
    InitializeComponent();
    
    string imagePath = ResolveImagePath();
    if (File.Exists(imagePath))
    {
        img = new Bitmap(imagePath);
    }
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: Error when i try to add my control to form1 Pin
_Q12_29-Jul-19 10:10
_Q12_29-Jul-19 10:10 
GeneralRe: Error when i try to add my control to form1 Pin
Richard Deeming30-Jul-19 1:52
mveRichard Deeming30-Jul-19 1:52 
GeneralRe: Error when i try to add my control to form1 Pin
_Q12_30-Jul-19 7:58
_Q12_30-Jul-19 7:58 
GeneralRe: Error when i try to add my control to form1 Pin
_Q12_29-Jul-19 10:34
_Q12_29-Jul-19 10:34 
GeneralRe: Error when i try to add my control to form1 Pin
_Q12_29-Jul-19 22:10
_Q12_29-Jul-19 22:10 
AnswerRe: Error when i try to add my control to form1 Pin
Dave Kreskowiak29-Jul-19 9:16
mveDave Kreskowiak29-Jul-19 9:16 
QuestionUnauthorizedAccessException Access to the remote share path Pin
Member 1158435626-Jul-19 0:09
Member 1158435626-Jul-19 0:09 
AnswerRe: UnauthorizedAccessException Access to the remote share path Pin
#realJSOP26-Jul-19 0:44
professional#realJSOP26-Jul-19 0:44 
Questionc# app design Pin
Member 1454172825-Jul-19 13:31
Member 1454172825-Jul-19 13:31 
AnswerRe: c# app design Pin
Gerry Schmitz25-Jul-19 14:42
mveGerry Schmitz25-Jul-19 14:42 
AnswerRe: c# app design Pin
Mycroft Holmes25-Jul-19 14:43
professionalMycroft Holmes25-Jul-19 14:43 
GeneralRe: c# app design Pin
#realJSOP26-Jul-19 0:47
professional#realJSOP26-Jul-19 0:47 
GeneralRe: c# app design Pin
Mycroft Holmes26-Jul-19 11:52
professionalMycroft Holmes26-Jul-19 11:52 
GeneralRe: c# app design Pin
MSBassSinger30-Jul-19 7:33
professionalMSBassSinger30-Jul-19 7:33 
AnswerRe: c# app design Pin
BillWoodruff25-Jul-19 15:10
professionalBillWoodruff25-Jul-19 15:10 
GeneralRe: c# app design Pin
Richard Andrew x6427-Jul-19 3:21
professionalRichard Andrew x6427-Jul-19 3:21 
QuestionCrystel Report in Print Duplicate Page Print Pin
Member 1235167123-Jul-19 0:53
Member 1235167123-Jul-19 0:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.