Taking the screenshot itself is relatively straightforward. Once the scene has been rendered, the Direct3D device offers a GetRenderTarget function that returns a pointer to a Direct3D surface with the current scene. Using a SurfaceToBitmap function, the RenderTarget can be converted to a Bitmap object, which can then be saved as either a JPEG or PNG to disk. Below is an example of the code involved:
Bitmap screenshot = Funcs.SurfaceToBitmap(device.GetRenderTarget(0));
screenshot.Save(Frame.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
screenshot.Dispose();
try
{
///
}
catch(InvalidOperationException ex)
{
throw new MyCustomException("Случилось страшное", ex);
}
catch(DataException ex)
{
throw new MyCustomException("Не менее страшное", ex);
}
catch (Exception ex)
{
throw new MyCustomException("Тут вообще хз что произошло", ex);
}
class ComplexValue
{
const string m_Alphabet = "abcdefghijklmnopqrstuvwxyz";
int First { get; set; }
char[] Middle { get; set; };
int Last { get; set; }
public ComplexValue(string input)
{
First = int.Parse(input.Substring(0, 1));
Middle = input.Substring(1, 3).ToCharArray();
Last = int.Parse(input.Substring(input.Length - 1, 1));
}
public string Value() => $"{First}{new string(Middle)}{Last}";
private int IncrementLast()
{
Last++;
if (Last > 9) { Last = 0; return 1; }
return 0;
}
private int DecrementLast()
{
Last--;
if (Last < 0) { Last = 9; return 1 }
return 0;
}
private char IncrementChar(char c, out bool carry)
{
carry = false;
c++;
if (c > m_Alphabet[m_Alphabet.Length -1] )
{
c = m_Alphabet[0];
carry = true;
}
return c;
}
private char DecrementChar(char c, out bool borrow)
{
borrow = false;
c--;
if (c < m_Alphabet[0])
{
c = m_Alphabet[m_Alphabet.Length - 1];
borrow = true;
}
return c;
}
private int IncrementMiddle()
{
int pos = Middle.Length - 1;
while (pos > 0)
{
Middle[pos] = IncrementChar(Middle[pos], out bool carry);
if (!carry) return 0;
pos--;
}
return 1;
}
private int DecrementMiddle()
{
int pos = Middle.Length - 1;
while (pos > 0)
{
Middle[pos] = DecrementChar(Middle[pos], out bool borrow);
if (!borrow) return 0;
pos--;
}
return 1;
}
public void Increment()
{
if (IncrementLast() > 0)
{
if (IncrementMiddle() > 0) First++;
}
}
public void Decrement()
{
if (DecrementLast() > 0)
{
if (DecrementMiddle() > 0) First--;
}
}
}