ResourceReadImage

ResourceCreate() 関数で作成されたまたはコンパイル中に EX5 ファイルに保存されたグラフィックリソースからデータを読みます。

bool  ResourceReadImage(
  const string      resource_name,       // 読まれるグラフィックリソース名
  uint&             data[],             // リソースからデータを受け取るための配列
  uint&             width,             // リソースの画像幅を受け取る
  uint&             height,             // リソースの画像の高さを受け取る
  );

パラメータ

resource_name

[in]  画像を含むグラフィクリソースの名称。自身のリソースにアクセスするには、「::ResourceName」が名称として使用されます。コンパイルされた EX5 ファイルからリソースをダウンロードする場合、完全な名称は「path\\filename.ex5::resourcename」のようにMQL5ディレクトリ、ファイル名とリソース名の相対パスが使用されるべきです 。

data[][]

[in]  グラフィックリソースからデータを受け取るための 1 次元または 2 次元の配列。

img_width

[out]  グラフィックリソース画像の幅(ピクセル単位)。

img_height

[out]  グラフィックリソース画像の高さ(ピクセル単位)。

戻り値

成功の場合は true、それ以外の場合は false 。エラー情報を取得するには、GetLastError() 関数が呼ばれます。

注意事項

data[] 配列がグラフィックリソースの作成に使用される場合, COLOR_FORMAT_ARGB_NORMALIZE または COLOR_FORMAT_XRGB_NOALPHA カラーフォーマットが使用されるべきです。

data[] 配列が 2 次元で、2 番目の次元が、グラフィカルリソースサイズのX(幅)より小さい場合、ResourceReadImage() 関数はfalseを返し、読み込みが行われません。しかし、もしリソースが存在すれば、実際の画像サイズが幅と高さのパラメータに返されます。 これで、リソースからのデータ受けとりをもう一度試むことが可能になります。

Example:

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
 {
//--- declare the parameters of the graphical resource
  string rc_name="Resource";
  uint   rc_width=100;
  uint   rc_height=100;
  uint   rc_data[];
  uint   rc_size=rc_width*rc_height;
 
  ResetLastError();
//--- set the size of the pixel array
  if(ArrayResize(rc_data,rc_size)!=rc_size)
    {
    Print("ArrayResize() failed. Error code: ",GetLastError());
    return;
    }
//--- fill the pixel array with a transparent color and create a graphical resource based on it
  ArrayInitialize(rc_data,0x00FFFFFF);
  if(!ResourceCreate(rc_name,rc_data,rc_width,rc_height,0,0,0,COLOR_FORMAT_ARGB_NORMALIZE))
    {
    Print("ResourceCreate() failed. Error code: ",GetLastError());
    return;
    }
  Print("Size of created recource array: ",rc_data.Size());
 
//--- check the created graphical resource.
//--- get the time and price data of the current bar
  MqlTick tick={};
  if(!SymbolInfoTick(Symbol(),tick))
    {
    Print("SymbolInfoTick() failed. Error code: ",GetLastError());
    return;
    }
//--- create the Bitmap object using the coordinates of the last tick price and time
  string obj_name="Bitmap";
  if(!ObjectCreate(0,obj_name,OBJ_BITMAP,0,tick.time,tick.bid))
    {
    Print("ObjectCreate() failed. Error code: ",GetLastError());
    return;
    }
//--- set the width and height of the created bitmap object equal to the width and height of the graphical resource.
//--- set the object anchor point to its center.
  ObjectSetInteger(0,obj_name,OBJPROP_XSIZE,rc_width);
  ObjectSetInteger(0,obj_name,OBJPROP_YSIZE,rc_height);
  ObjectSetInteger(0,obj_name,OBJPROP_ANCHOR,ANCHOR_CENTER);
//--- specify the previously created graphical resource for the bitmap object as an image file
//--- In this case, in order to indicate the name of the graphical resource used, we need to add "::" before its name
  ObjectSetString(0,obj_name,OBJPROP_BMPFILE,"::"+rc_name);
 
//--- set the DodgerBlue color with the transparency of 200
  uint clr=ColorToARGB(clrDodgerBlue,200);
//--- fill the entire array of pixels of the graphical resource with the set color
  ArrayInitialize(rc_data,clr);
//--- update the graphical resource data
  Update(rc_name,rc_data,rc_width,rc_height,true);
 
//--- wait 3 seconds before reading the graphical resource data
  Print("Wait 3 seconds before ResourceReadImage()");
  Sleep(3000);
//--- read the image from the resource into a new array of pixels
  uint rc_data_copy[];
  uint w=0,h=0;
  ResetLastError();
  if(!ResourceReadImage("::"+rc_name,rc_data_copy,w,h))
    {
    Print("ResourceReadImage() failed. Error code: ",GetLastError());
    return;
    }
 
//--- set the OrangeRed color with the transparency of 200
  clr=ColorToARGB(clrOrangeRed,200);
//--- fill the entire array of pixels of the graphical resource with the set color and create a new graphical resource based on it
  ArrayInitialize(rc_data_copy,clr);
  if(!ResourceCreate(rc_name+"Copy",rc_data_copy,rc_width,rc_height,0,0,0,COLOR_FORMAT_ARGB_NORMALIZE))
    {
    Print("New ResourceCreate() failed. Error code: ",GetLastError());
    return;
    }
  Print("Size of created new recource array: ",rc_data_copy.Size());
 
//--- create the "Graphical label" object using the coordinates of the last tick price and time
  string obj_name2="BitmapLabel";
  if(!ObjectCreate(0,obj_name2,OBJ_BITMAP_LABEL,0,0,0))
    {
    Print("ObjectCreate() failed. Error code: ",GetLastError());
    return;
    }
//--- get screen coordinates using the previously received price and time
  int x=0,y=0;
  if(!ChartTimePriceToXY(0,0,tick.time,tick.bid,x,y))
    {
    Print("New ChartTimePriceToXY() failed. Error code: ",GetLastError());
    return;
    }
//--- set the width and height of the created graphical label object equal to the width and height of the graphical resource.
//--- set the object anchor point to its center.
  ObjectSetInteger(0,obj_name2,OBJPROP_XSIZE,rc_width);
  ObjectSetInteger(0,obj_name2,OBJPROP_YSIZE,rc_height);
  ObjectSetInteger(0,obj_name2,OBJPROP_ANCHOR,ANCHOR_LEFT_UPPER);
  ObjectSetInteger(0,obj_name2,OBJPROP_XDISTANCE,x);
  ObjectSetInteger(0,obj_name2,OBJPROP_YDISTANCE,y);
//--- set the copied graphical resource as an image file for the graphical label object
//--- in this case, in order to indicate the name of the graphical resource used, we need to add "::" before its name
  ObjectSetString(0,obj_name2,OBJPROP_BMPFILE,"::"+rc_name+"Copy");
 
//--- change the color of the new graphical label object
  Print("Wait 3 seconds before changing color to GreenYellow");
  Sleep(3000);
//--- set the GreenYellow color with the transparency of 200
  clr=ColorToARGB(clrGreenYellow,200);
//--- fill the entire array of pixels of the new graphical resource with the set color
  ArrayInitialize(rc_data_copy,clr);
//--- update the graphical resource data
  Update(rc_name+"Copy",rc_data_copy,rc_width,rc_height,true);
 
//--- wait three seconds and delete resources and both objects
  Print("Wait 3 seconds before deleting both objects");
  Sleep(3000);
  Print("Deleting Resource and all Bitmap objects");
  ResourceFree("::"+rc_name);
  ResourceFree("::"+rc_name+"Copy");
  ObjectDelete(0,obj_name);
  ObjectDelete(0,obj_name2);
 }
//+------------------------------------------------------------------+
//| Update graphical resource data                                   |
//+------------------------------------------------------------------+
void Update(const string res_name,const uint &pixel_data[],const uint width,const uint height,const bool redraw)
 {
//--- leave if zero dimensions are passed
  if(width==0 || height==0)
    return;
//--- update resource data and redraw the chart
  if(ResourceCreate(res_name,pixel_data,width,height,0,0,0,COLOR_FORMAT_ARGB_NORMALIZE) && redraw)
    ChartRedraw();
 }

参照

リソースObjectCreate()ObjectSetString()OBJPROP_BMPFILE