First of all, I am trying to show an image on my chart when I click a button and remove that image when I click the button again. I have taken some sample code from this forum and NT8 help guide. Codes are like below:
protected override void OnBarUpdate()
{
}
private SharpDX.Direct2D1.Bitmap myBitmap = null;
public override void OnRenderTargetChanged()
{
myBitmap = LoadBitmapFromContentFile(System.IO.Path.Combine(N injaTrader.Core.Globals.UserDataDir, "MyImage.png")); // Type the file location depending upon the folder and file location of the file
}
private SharpDX.Direct2D1.Bitmap LoadBitmapFromContentFile(string filePath)
{
if (RenderTarget == null) return null;
SharpDX.Direct2D1.Bitmap newBitmap;
// Neccessary for creating WIC objects.
SharpDX.WIC.ImagingFactory imagingFactory = new SharpDX.WIC.ImagingFactory();
SharpDX.IO.NativeFileStream fileStream = new SharpDX.IO.NativeFileStream(filePath, SharpDX.IO.NativeFileMode.Open, SharpDX.IO.NativeFileAccess.Read);
// Used to read the image source file.
SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder(imagingFactory, fileStream, SharpDX.WIC.DecodeOptions.CacheOnDemand);
// Get the first frame of the image.
SharpDX.WIC.BitmapFrameDecode frame = bitmapDecoder.GetFrame(0);
// Convert it to a compatible pixel format.
SharpDX.WIC.FormatConverter converter = new SharpDX.WIC.FormatConverter(imagingFactory);
converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPRGBA);
// Create the new Bitmap directly from the FormatConverter.
if (RenderTarget != null) newBitmap = SharpDX.Direct2D1.Bitmap.FromWicBitmap(RenderTarge t, converter);
else newBitmap = null;
SharpDX.Utilities.Dispose(ref bitmapDecoder);
SharpDX.Utilities.Dispose(ref fileStream);
SharpDX.Utilities.Dispose(ref imagingFactory);
return newBitmap;
}
private void OnMyMultiChartButtonClick(object sender, RoutedEventArgs rea)
{
System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
if (button != null)
{
myBitmap = LoadBitmapFromContentFile(System.IO.Path.Combine(N injaTrader.Core.Globals.UserDataDir, "MyImage.png"));
float startPoint = ChartPanel.X;
float endPoint = (ChartPanel.Y + ChartPanel.H / 2);
//SharpDX.RectangleF rect = new SharpDX.RectangleF(startPoint.X, startPoint.Y, width, height);
RenderTarget.DrawBitmap(myBitmap, new SharpDX.RectangleF(startPoint, endPoint, (float)ChartPanel.W * 0.4f, (float)ChartPanel.H / 2), 1.0f, SharpDX.Direct2D1.BitmapInterpolationMode.Linear);
}
}
It looks ok to me, but I think, something is missing somewhere, because it's not executing ie, after I click the button it does not display the MyImage.png.
Secondly, I wanted to remove the image from the chart window.
Can anyone guide me on this please how to resolve this issue?
thanks.
Comment