0

I am new to C#, now using Canon's EDSDK having a live-view of the camera in a pictureBox. How is it possible to rotate the live-view image by 180°?

I tried rotating the image after the liveView is established


MainForm.cs

Camera MainCamera;  // is instanciated elsewhere
public void startLiveView() {

    MainCamera.StartLiveView();

    // at this point, live-view is working and LiveViewPicBox contains the live-view
    // this code has no effect:
    Image img = LiveViewPicBox.Image;
    img.RotateFlip(RotateFlipType.Rotate180FlipY);
    LiveViewPicBox.Image = img;
}

Camera.cs

public void StartLiveView()
{
    CheckState();
    if (!IsLiveViewOn) SetSetting(PropertyID.Evf_OutputDevice, (int)EvfOutputDevice.PC);
}

public void SetSetting(PropertyID propID, object value, int inParam = 0)
{
    CheckState();

    MainThread.Invoke(() =>
    {
        int propsize;
        DataType proptype;
        ErrorHandler.CheckError(this, CanonSDK.EdsGetPropertySize(CamRef, propID, inParam, out proptype, out propsize));
        ErrorHandler.CheckError(this, CanonSDK.EdsSetPropertyData(CamRef, propID, inParam, propsize, value));
    });
}

SDKMethods.cs (CanonSDK)

[DllImport(DllPath)]
public extern static ErrorCode EdsGetPropertySize(IntPtr inRef, PropertyID inPropertyID, int inParam, out DataType outDataType, out int outSize);


[DllImport(DllPath)]
public extern static ErrorCode EdsSetPropertyData(IntPtr inRef, PropertyID inPropertyID, int inParam, int inPropertySize, [MarshalAs(UnmanagedType.AsAny), In] object inPropertyData);

The Live-View is working but no rotation is applied.

Note: The pictureBox has property WaitOnLoad=false

I assume there's some kind of image-stream which I'd need to rotate instead, though I do not understand much of the code in the SDK. Can anyone assist me, telling me where to start?

Jonathan
  • 1,955
  • 5
  • 30
  • 50
  • Is this a WinForms application? Are you able to show more details about how you created the LiveViewPicBox (properties, etc). What event are you trying to use your code in? Is that all the code in the event that accesses the image or picture box? – Hayden Nov 15 '19 at 11:31
  • I updated my question with more detailed code. It's a WinForms application. – Jonathan Nov 15 '19 at 11:39
  • `LiveViewPicBox` is a simple picturebox, no special properties assigned. It's the `EDSDK` example I am working with. A button click triggers `MainForm.startLiveView()` – Jonathan Nov 15 '19 at 11:45
  • To be honest, I don't even see where the actual image(stream?) is set on the picturebox in the SDK code. No matter how deep I dig. – Jonathan Nov 15 '19 at 11:47
  • The code you posted to rotate the image is correct. This can be verified by making a new WinForms application with a PictureBox with an image in it, and a button with the code to rotate it. My hunch is that the PictureBox is not being updated due to the LiveView not being done on the UI thread. – Hayden Nov 15 '19 at 11:48
  • You are right. I assume the rotation has to be applied somehwere inside the SDK. It's essential for me to get this feature working. Is there any (any) way to rotate the final picturebox-control? (dirty workaround accepted) – Jonathan Nov 15 '19 at 11:50

1 Answers1

2

It looks like you are using my tutorial from CodeProject. In that case you are looking at the wrong thing. There are two relevant methods in the example, the LiveViewUpdated event handler where the current live view frame is passed (called MainCamera_LiveViewUpdated) and the picture box paint event where live view is actually drawn (called LiveViewPicBox_Paint)

In MainCamera_LiveViewUpdated the current frame is read into a Bitmap and the picture box is "notified" that it should redraw itself.

In the LiveViewPicBox_Paint method you'll see that the picture box image isn't actually used but the image is drawn onto the control directly using a Graphics object passed to you by the PaintEventArgs (this is done for performance reasons).

Now, that you have both the Graphics object as well as the live view frame, you can draw it any way you like. For rotation, have a look at this answer here: Rotating graphics?

Johannes Bildstein
  • 1,069
  • 2
  • 8
  • 20
  • Amazing **you** found this question! Sadly, I neither find `SDK_LiveViewUpdated` nor `CreateGraphics` anywhere in the project. (But found this line: `//Update live view` `LiveViewUpdated?.Invoke(this, stream);`) – Jonathan Nov 15 '19 at 13:13
  • 1
    Sorry @Jonathan, I was looking the wrong example, I have updated the answer for the correct one now. This all happens in the MainForm.cs file btw. Also, I get notifications for Canon SDK questions ;) – Johannes Bildstein Nov 15 '19 at 13:26