C# System.Drawing Graphic Not implemented exception - graphics

The following code compiles, but when the method is called I get System.NotImplementedException: Not implemented. I'm not using custom classes.
private void miterTopLeft(Image img, Graphics gfx)
{
int maxSize = Math.Max(img.Height, img.Width);
Point[] points = new Point[4];
points[0] = new Point(0, 0);
points[1] = new Point(maxSize, maxSize);
points[2] = new Point(0, maxSize);
points[3] = new Point(0, 0);
gfx.DrawImage(img, points);
}
Why is this error occuring in a .NET class? Is there a work around?
Stack Trace:
[NotImplementedException: Not implemented.]
System.Drawing.Graphics.CheckErrorStatus(Int32 status) +1154064
System.Drawing.Graphics.DrawImage(Image image, PointF[] destPoints) +150
GetMergeImage.miterTopLeft(Image img, Graphics gfx) in d:\.NET Projects\publish2\GetMergeImage.ashx:51
GetMergeImage.drawLeftBorderRectangeRug(Double ppi, Boolean hasCorner, Graphics gfx, DesignVO border, Image img, Double vBorderStartY, Double vBorderStartX, Double vBorderNumRepeat) in d:\.NET Projects\publish2\GetMergeImage.ashx:243
GetMergeImage.drawRectangularRug(Int32 displayWidth, Int32 displayHeight, Int32 width, Int32 height, MergeVO merge) in d:\.NET Projects\publish2\GetMergeImage.ashx:140
GetMergeImage.ProcessRequest(HttpContext context) in d:\.NET Projects\publish2\GetMergeImage.ashx:40
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +100
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

According to the documentation you can only pass three points that make up a parallelogram.
If you want a triangle you will need to clip the drawing

Related

Why can't I render onto a Skia canvas holding a 8 bit Grayscale bitmap

I am trying to do some basic drawing with skia. Since I'm working on grayscale images I want to use the corresponding color type. The minimal Example I want to use is:
int main(int argc, char * const argv[])
{
int width = 1000;
int heigth = 1000;
float linewidth = 10.0f;
SkImageInfo info = SkImageInfo::Make(
width,
heigth,
SkColorType::kAlpha_8_SkColorType,
SkAlphaType::kPremul_SkAlphaType
);
SkBitmap img;
img.allocPixels(info);
SkCanvas canvas(img);
canvas.drawColor(SK_ColorBLACK);
SkPaint paint;
paint.setColor(SK_ColorWHITE);
paint.setAlpha(255);
paint.setAntiAlias(false);
paint.setStrokeWidth(linewidth);
paint.setStyle(SkPaint::kStroke_Style);
canvas.drawCircle(500.0f, 500.0f, 100.0f, paint);
bool success = SkImageEncoder::EncodeFile("B:\\img.png", img,
SkImageEncoder::kPNG_Type, 100);
return 0;
}
But the saved image does not contain the circle that was drawn. If I replace kAlpha_8_SkColorType with kN32_SkColorType I get the expected result. How can I draw the circle onto a 8 bit grayscale image? I'm working with Visual Studio 2013 on a 64bit Windows machine.
kN32_SkColorType type result
kAlpha_8_SkColorType result
You should use kGray_8_SkColorType than kAlpha_8_SkColorType.
The kAlpha_8_SkColorType used for bitmap mask.

StretchBlt giving monochrome bitmap as output

I m writing code for scaling down the image in VC++. When i save destination bitmap it gives monochrome image.
Code:
int iNewWidth = 400;
int iNewHeight = 500;
CImage image;
HRESULT rs=image.Load(_T("E:\\input.jpg"));
int a=10;
CDC destDC;
int res=destDC.CreateCompatibleDC(NULL);
HDC hdcDest=HDC(destDC);
HBITMAP hDestBitmap=CreateCompatibleBitmap(hdcDest, iNewWidth, iNewHeight);
HBITMAP hOldBitmap=(HBITMAP)SelectObject(hdcDest,hDestBitmap);
SetStretchBltMode(hdcDest,BLACKONWHITE);
BOOL bl=image.StretchBlt(hdcDest,0, 0, iNewWidth, iNewHeight, 0, 0, image.GetWidth(), image.GetHeight(), SRCERASE);
HRESULT res2;
CImage new_image;
new_image.Attach(hDestBitmap);
res2=new_image.Save(_T("E:\\NewImage.jpg"));
HBITMAP hb=new_image.Detach();
ReleaseDC(NULL,hdcDest);
I will be very thankful if someone help me here.
Thanks in advance
When you create a DC it initially has a monochrome bitmap selected into it. When you use CreateCompatibleBitmap with that DC, it uses the characteristics of that selected bitmap - i.e. it creates another monochrome bitmap.
Use the DC returned by GetDC(NULL) in your CreateCompatibleBitmap call.

Compositing 2 images in J2ME

I intend to fit an image centered horizontally and vertically inside a J2ME form. However I couldn't find useful markup elements to do so. So I intend to create one totally transparent image the size of the form element and superimpose my intended image on it centered. And place the resulting image in the form (without using a canvas). I am looking for ways of doing this because my knowledge of J2ME is limited.
Any help, please?
public static Image CreateCompositeImage(Image oImage,int formWidth,int formHeight){
final int imageWidth=oImage.getWidth();
final int imageHeight=oImage.getHeight();
int[] imge=new int[imageWidth*imageHeight];
oImage.getRGB(imge,0,imageWidth,0,0,imageWidth,imageHeight);
final int topMargin=(formHeight-imageHeight)/2;
final int leftMargin=(formWidth-imageWidth)/2;
final int pixelTop=topMargin*formWidth;
int[] c=new int[formWidth*formHeight];
int p=0, r=0;
for (int i=0;i<pixelTop;i++){
c[p++]=0xff000000;
}
for (int j=0;j<imageHeight;j++){
for (int i=0;i<leftMargin;i++){
c[p++]=0x880000ff;
}
for (int i=0;i<imageWidth;i++){
c[p++]=imge[r++];
}
for (int i=0;i<leftMargin;i++){
c[p++]=0x8800ff00;
}
}
int pixelBottom=formWidth*formHeight-p;
for (int i=0;i<pixelBottom;i++){
c[p++]=0xffffffff;
}
return Image.createRGBImage(c,formWidth,formHeight,true);
}
A better approach is to create new class that inherits from CustomItem, or to use a Canvas instead of the Form.
In both cases you override the paint() method.
There you get a Graphics-Object. You use this object do do your drawing.
Especially for you it has a drawImage() method, where you can just put in the position.
You then need no pixel data manipulation.
Overriding CustomItem or Canvas is something you do often in Java-me programming, so it is worth learn it.

Using DirectX 9; Sprite Anti-Aliasing Halo

So I am coding in DirectX 9 and whenever I place a sprite inside of a 2D world. There is a white colored "halo" that appears around the sprite image p. I am using PNGs and the background behind the sprite is transparent. I have also tried using a pink background as well. It seems that the halo only appears on straight lines of pixels but only on some edges. Any help is greatly appreciated!
m_d3d = Direct3DCreate9(D3D_SDK_VERSION); // create the Direct3D interface
D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information
ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear out the struct for use
d3dpp.Windowed = windowed; // is program fullscreen, not windowed?
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames
d3dpp.hDeviceWindow = hWnd; // set the window to be used by Direct3D
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; // set the back buffer format to 32-bit
d3dpp.BackBufferWidth = screenWidth; // set the width of the buffer
d3dpp.BackBufferHeight = screenHeight; // set the height of the buffer
d3dpp.EnableAutoDepthStencil = TRUE; // automatically run the z-buffer for us
d3dpp.AutoDepthStencilFormat = D3DFMT_D16; // 16-bit pixel format for the z-buffer
// create a device class using this information and the info from the d3dpp stuct
m_d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&m_d3ddev);
D3DXCreateSprite(m_d3ddev, &m_d3dspt); // create the Direct3D Sprite object
LPDIRECT3DTEXTURE9 texture;
D3DXCreateTextureFromFileEx(m_d3ddev, "DC.png", D3DX_DEFAULT, D3DX_DEFAULT,
D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT,
D3DX_DEFAULT, D3DCOLOR_XRGB(255, 0, 255), NULL, NULL, &texture);
m_d3ddev->BeginScene();
m_d3dspt->Begin(D3DXSPRITE_ALPHABLEND); // begin sprite drawing with transparency
D3DXVECTOR3 center(0.0f, 0.0f, 0.0f), position((appropriate x), (appropriate y), 1);
m_d3dspt->Draw(texture, NULL, &center, &position, D3DCOLOR_ARGB(255, 255, 255, 255));
m_d3dspt->End(); // end sprite drawing
m_d3ddev->EndScene();
m_d3ddev->Present(NULL, NULL, NULL, NULL);
Thanks
Peter
This occurs when you screw up your texture co-ordinates from sprite atlasing and you accidentally run off the texture or on to another texture.
Most commonly, anyway, AFAIK.

Object disposed exception to be thrown

I have recently integrated in a HUD method into my XNA game project and when the method is called by the main Draw method it throws out a object disposed exception this has something to do with the two Drawstring used in the program.
The exception is thrown at spriteBatch.End() and says Cannot access a disposed object.
Object name: 'Texture2D'.
//initiation of the spritebatch
private SpriteBatch spriteBatch;
//game draw method
public override void Draw(GameTime gameTime)
{
ScreenManager.GraphicsDevice.Clear(Color.CornflowerBlue);
// Our player and enemy are both actually just text strings.
spriteBatch = ScreenManager.SpriteBatch;
tileMap.Draw(spriteBatch, camera);
spriteBatch.Begin(SpriteSortMode.Deferred,
BlendState.AlphaBlend,
null, null, null, null,
camera.TransformMatrix);
DrawHud();
level.Draw(gameTime, spriteBatch);
spriteBatch.End();
// If the game is transitioning on or off, fade it out to black.
if (TransitionPosition > 0 || pauseAlpha > 0)
{
float alpha = MathHelper.Lerp(1f - TransitionAlpha, 1f, pauseAlpha / 2);
ScreenManager.FadeBackBufferToBlack(alpha);
}
base.Draw(gameTime);
}
the HUD method
private void DrawHud()
{
Rectangle titleSafeArea = ScreenManager.GraphicsDevice.Viewport.TitleSafeArea;
Vector2 hudLocation = new Vector2(titleSafeArea.X + camera.Position.X, titleSafeArea.Y + camera.Position.Y);
Vector2 center = new Vector2(titleSafeArea.Width + camera.Position.X / 2.0f,
titleSafeArea.Height + camera.Position.Y / 2.0f);
// Draw time remaining. Uses modulo division to cause blinking when the
// player is running out of time.
string timeString = "TIME: " + level.TimeRemaining.Minutes.ToString("00") + ":" + level.TimeRemaining.Seconds.ToString("00");
Color timeColor;
if (level.TimeRemaining > WarningTime ||
level.ReachedExit ||
(int)level.TimeRemaining.TotalSeconds % 2 == 0)
{
timeColor = Color.Yellow;
}
else
{
timeColor = Color.Red;
}
DrawShadowedString(hudFont, timeString, hudLocation, timeColor);
// Draw score
float timeHeight = hudFont.MeasureString(timeString).Y;
DrawShadowedString(hudFont, "SCORE: " + level.Score.ToString(), hudLocation + new Vector2(0.0f, timeHeight * 1.2f), Color.Yellow);
}
//method which draws the score and the time (and is causing the problem)
private void DrawShadowedString(SpriteFont font, string value, Vector2 position, Color color)
{
spriteBatch.DrawString(font, value, position + new Vector2(1.0f, 1.0f), Color.Black);
spriteBatch.DrawString(font, value, position, color);
}
As the exception says, the problem exists because one of the Texture2Ds you are using is being disposed before you are using it.
There are two things in the XNA API (that come to mind) that will dispose of a Texture2D: The ContentManager.Unload() method for any textures loaded by that content manager, and the Texture2D.Dispose() method. So check if your own code is calling one of these two functions at any point.
The exception will only be thrown when the Texture2D instance is "used". Because SpriteBatch batches together texture draws, the texture doesn't actually get used until you end the SpriteBatch (at which point it draws everything in one go). If you change to SpriteSortMode.Immediate SpriteBatch will stop batching sprites and will instead draw them "immediately" you ask it to. This will cause the texture to be used and the exception to be thrown at a Draw call instead of an End call, which should make it easier to identify which texture is being disposed of while still in use.
The code you have posted seems to be fine, I suspect the problem exists elsewhere in your code. The above information should help you identify where the problem is.
My guess is that something is happening in level.Draw that is disposing of a texture somewhere. It doesn't look like the drawhud method in particular is responsible
You mention though that you are sure it's caused by the drawstring methods ... if you comment those two out in particular does the error go away?

Resources