PNG image on the MT4 Chart is not working!

 

Hello,

I'm tryng to show png image on the chart when i do run the EA, below the related script:

int init() {
    ShowImage("image.png", 0, 0);

    return INIT_SUCCEEDED;
}

void OnDeinit(const int reason)
  {
   ObjectDelete("ChartImage");
  }
  
void ShowImage(string imageName, int xOffset, int yOffset)
  {
   string objectName = "ChartImage";
   
   ObjectDelete(objectName);
   
   if(!ObjectCreate(0, objectName, OBJ_BITMAP_LABEL, 0, 0, 0))
     {
      Print("Failed to create the object: ", objectName);
      return;
     }
   
   // Set the image properties
   ObjectSetInteger(0, objectName, OBJPROP_XSIZE, 500);
   ObjectSetInteger(0, objectName, OBJPROP_YSIZE, 500);
   ObjectSetString(0, objectName, OBJPROP_TEXT, imageName);
   ObjectSetInteger(0, objectName, OBJPROP_CORNER, CORNER_LEFT_UPPER);
   ObjectSetInteger(0, objectName, OBJPROP_XDISTANCE, xOffset);
   ObjectSetInteger(0, objectName, OBJPROP_YDISTANCE, yOffset);
  }

Objects List screenshot


as you see in the attached screenshot, it is showing the object in the objects list, but not showing it in the chart actually.


please advise where is the wrong ?

 

You should study the documentation. Just a glance and I realized there is a property called: OBJPROP_BMPFILE.

You can Also check this topic.

OBJ_BITMAP_LABEL - Object Types - Objects Constants - Constants, Enumerations and Structures - MQL4 Reference
OBJ_BITMAP_LABEL - Object Types - Objects Constants - Constants, Enumerations and Structures - MQL4 Reference
  • docs.mql4.com
OBJ_BITMAP_LABEL - Object Types - Objects Constants - Constants, Enumerations and Structures - MQL4 Reference
 
Yashar Seyyedin #:

You should study the documentation. Just a glance and I realized there is a property called: OBJPROP_BMPFILE.

You can Also check this topic.

thank you for your reply, i have applied but now i can see the issue is:

Error opening directory: 5002

the files are there and i'm sure the permissions are ok and below my new script with full debugging:

void ShowOrderTypeImage()
{
    string dataPath = TerminalInfoString(TERMINAL_DATA_PATH);
    string bullImagePath = dataPath + "\\MQL4\\Images\\bull.bmp";
    Print("Image path: ", bullImagePath);

    // Check if the directory exists
    string imageDir = dataPath + "\\MQL4\\Images";
    if (!DirectoryExists(imageDir))
    {
        Print("Directory does not exist: ", imageDir);
        return;
    }

    // Check if the BMP file exists
    if (FileExistsCustom(bullImagePath))
    {
        Print("BMP file exists: ", bullImagePath);
    }
    else
    {
        Print("BMP file does not exist: ", bullImagePath);
        PrintDirectoryListing(imageDir);
        return;
    }

    // Delete any existing image labels to avoid overlapping
    if (ObjectFind(0, "OrderTypeImage") != -1)
    {
        if (!ObjectDelete(0, "OrderTypeImage"))
        {
            Print("Error deleting existing OrderTypeImage: ", GetLastError());
        }
        else
        {
            Print("Successfully deleted existing OrderTypeImage");
        }
    }

    // Display the bull image in the middle of the chart
    if (!ObjectCreate(0, "OrderTypeImage", OBJ_BITMAP_LABEL, 0, 0, 0))
    {
        Print("Error creating OrderTypeImage: ", GetLastError());
        return;
    }
    else
    {
        Print("Successfully created OrderTypeImage");
    }

    if (!ObjectSetString(0, "OrderTypeImage", OBJPROP_BMPFILE, bullImagePath))
    {
        Print("Error setting BMP file for OrderTypeImage: ", GetLastError());
    }
    else
    {
        Print("Successfully set BMP file for OrderTypeImage");
    }

    // Get the chart width and height to position the image in the center
    long chartWidth = ChartGetInteger(0, CHART_WIDTH_IN_PIXELS); // Use long to avoid loss of data
    long chartHeight = ChartGetInteger(0, CHART_HEIGHT_IN_PIXELS); // Use long to avoid loss of data
    Print("Chart width: ", chartWidth, ", Chart height: ", chartHeight);

    if (!ObjectSetInteger(0, "OrderTypeImage", OBJPROP_XDISTANCE, (int)(chartWidth / 2 - 150))) // Center horizontally
    {
        Print("Error setting X distance for OrderTypeImage: ", GetLastError());
    }
    else
    {
        Print("Successfully set X distance for OrderTypeImage");
    }

    if (!ObjectSetInteger(0, "OrderTypeImage", OBJPROP_YDISTANCE, (int)(chartHeight / 2 - 150))) // Center vertically
    {
        Print("Error setting Y distance for OrderTypeImage: ", GetLastError());
    }
    else
    {
        Print("Successfully set Y distance for OrderTypeImage");
    }

    if (!ObjectSetInteger(0, "OrderTypeImage", OBJPROP_XSIZE, 300)) // Adjust the width as needed
    {
        Print("Error setting X size for OrderTypeImage: ", GetLastError());
    }
    else
    {
        Print("Successfully set X size for OrderTypeImage");
    }

    if (!ObjectSetInteger(0, "OrderTypeImage", OBJPROP_YSIZE, 300)) // Adjust the height as needed
    {
        Print("Error setting Y size for OrderTypeImage: ", GetLastError());
    }
    else
    {
        Print("Successfully set Y size for OrderTypeImage");
    }

    if (!ObjectSetInteger(0, "OrderTypeImage", OBJPROP_HIDDEN, false))
    {
        Print("Error setting hidden property for OrderTypeImage: ", GetLastError());
    }
    else
    {
        Print("Successfully set hidden property for OrderTypeImage");
    }

    if (!ObjectSetInteger(0, "OrderTypeImage", OBJPROP_SELECTABLE, true))
    {
        Print("Error setting selectable property for OrderTypeImage: ", GetLastError());
    }
    else
    {
        Print("Successfully set selectable property for OrderTypeImage");
    }

    if (!ObjectSetInteger(0, "OrderTypeImage", OBJPROP_SELECTED, true))
    {
        Print("Error setting selected property for OrderTypeImage: ", GetLastError());
    }
    else
    {
        Print("Successfully set selected property for OrderTypeImage");
    }
}

// Function to check if a file exists
bool FileExistsCustom(string filename)
{
    int handle = FileOpen(filename, FILE_READ | FILE_BIN);
    if (handle != INVALID_HANDLE)
    {
        FileClose(handle);
        return true;
    }
    else
    {
        Print("FileOpen error: ", GetLastError(), " for file: ", filename);
    }
    return false;
}

// Function to check if a directory exists
bool DirectoryExists(string directoryPath)
{
    string fileName;
    int handle = FileFindFirst(directoryPath + "\\*", fileName);
    if (handle != INVALID_HANDLE)
    {
        FileFindClose(handle);
        return true;
    }
    return false;
}

// Function to print directory listing
void PrintDirectoryListing(string directoryPath)
{
    Print("Listing files in directory: ", directoryPath);
    string fileName;
    int handle = FileFindFirst(directoryPath + "\\*.bmp", fileName);
    if (handle == INVALID_HANDLE)
    {
        Print("Error opening directory: ", GetLastError());
        return;
    }

    do
    {
        Print("Found file: ", fileName);
    }
    while (FileFindNext(handle, fileName));

    FileFindClose(handle);
}

 
Anass Habrah #: Error opening directory: 5002

the files are there and i'm sure the permissions are ok and below my new script with full debugging:

    string dataPath = TerminalInfoString(TERMINAL_DATA_PATH);
    string bullImagePath = dataPath + "\\MQL4\\Images\\bull.bmp";

The path you passed is garbage.

You can't read (or write) outside the sandbox with normal code.
          File Write Problem (Error: 5002) - Expert Advisors and Automated Trading - MQL5 programming forum #1-2 (2020)

For security reasons, work with files is strictly controlled in the MQL5 language. Files with which file operations are conducted using MQL5 means, cannot be outside the file sandbox.
          FileOpen - File Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

 
Anass Habrah:

Hello,

I'm tryng to show png image on the chart when i do run the EA, below the related script:


as you see in the attached screenshot, it is showing the object in the objects list, but not showing it in the chart actually.


please advise where is the wrong ?

https://www.mql5.com/en/code/45439
If you want to include png file in ex4, then unfortunately in Mql4 there is no such possibility as in mql5:

#resource "//Images//image.png" as uchar image[]

but there is a possibility:


PNG
PNG
  • www.mql5.com
Forget about BMP files like a bad dream. Thanks to this library, you can now use the PNG format, which has a number of advantages, such as being more compact without losing image quality and maintaining transparency.
Files:
MQL4.ZIP  785 kb