/// <summary>
/// Gets the image from a Zwo camera
/// </summary>
/// <param name="exposureTimeSeconds">The exposure time in milliseconds.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The image returned data array.</returns>
private async Task<int> GetImageFromZwoCamera(double exposureTimeSeconds, CancellationToken cancellationToken)
{
var task = Task.Run(
async () =>
{
// Set camera parameters
CaptureAreaInfo area = this.asiCamera.CaptureAreaInfo;
int imageWidth = area.Size.Width / this.settingsEntity.BinningFactor;
int imageHeight = area.Size.Height / this.settingsEntity.BinningFactor;
ASICameraDll2.ASI_IMG_TYPE imageTypeToUse = this.settingsEntity.ZwoCameraUseRaw8InstedOfRaw16 ? ASICameraDll2.ASI_IMG_TYPE.ASI_IMG_RAW8 : ASICameraDll2.ASI_IMG_TYPE.ASI_IMG_RAW16;
this.asiCamera.CaptureAreaInfo = new CaptureAreaInfo(new Size(imageWidth, imageHeight), this.settingsEntity.BinningFactor, imageTypeToUse);
// USB Limit
var bandwidthControl = this.asiCamera.GetControl(ASICameraDll2.ASI_CONTROL_TYPE.ASI_BANDWIDTHOVERLOAD);
bandwidthControl.Value = this.settingsEntity.ZwoCameraUsbLimit;
// Set Gain
var gainControl = this.asiCamera.GetControl(ASICameraDll2.ASI_CONTROL_TYPE.ASI_GAIN);
gainControl.Value = this.settingsEntity.ZwoCameraGain;
// Set Offset
var offsetControl = this.asiCamera.GetControl(ASICameraDll2.ASI_CONTROL_TYPE.ASI_BRIGHTNESS);
offsetControl.Value = this.settingsEntity.ZwoCameraOffset;
// Exposure Time
var exposureTimeControl = this.asiCamera.GetControl(ASICameraDll2.ASI_CONTROL_TYPE.ASI_EXPOSURE);
exposureTimeControl.Value = (int)(exposureTimeSeconds * 1000000);
// White Balance
var redBalanceControl = this.asiCamera.GetControl(ASICameraDll2.ASI_CONTROL_TYPE.ASI_WB_R);
redBalanceControl.Value = this.settingsEntity.ZwoCameraRedBalance;
var blueBalanceControl = this.asiCamera.GetControl(ASICameraDll2.ASI_CONTROL_TYPE.ASI_WB_B);
blueBalanceControl.Value = this.settingsEntity.ZwoCameraBlueBalance;
// Start exposure
this.asiCamera.StartExposure(false);
while (this.asiCamera.ExposureStatus == ASICameraDll2.ExposureStatus.ExpWorking)
{
await Task.Delay(new TimeSpan(0, 0, 0, 0, 200), cancellationToken);
}
if (this.asiCamera.ExposureStatus == ASICameraDll2.ExposureStatus.ExpSuccess)
{
byte buffer;
if (this.asiCamera.CaptureAreaInfo.ImageType == ASICameraDll2.ASI_IMG_TYPE.ASI_IMG_RAW8)
{
buffer = this.CopyCameraDataToBuffer(this.asiCamera.CaptureAreaInfo.Size.Height * this.asiCamera.CaptureAreaInfo.Size.Width);
return buffer.Select(x => (int)x).ToArray();
}
if (this.asiCamera.CaptureAreaInfo.ImageType == ASICameraDll2.ASI_IMG_TYPE.ASI_IMG_RAW16)
{
buffer = this.CopyCameraDataToBuffer(this.asiCamera.CaptureAreaInfo.Size.Height * this.asiCamera.CaptureAreaInfo.Size.Width * 2);
int returnArray = new int;
for (int index = 0; index < buffer.Length - 1; index = index + 2)
{
byte lowerByte = buffer;
byte higherByte = buffer;
returnArray = 256 * (int)higherByte + (int)lowerByte;
}
return returnArray;
}
throw new ApplicationException("Application only supports RAW8 and RAW16 formats for ZWO Cameras.");
}
throw new ApplicationException("Exposure Failed.");
});
await task;
return task.Result;
}