GDI+ round corner image c++ - c++

I'm using GDI+ to draw image on a CStatic *element using this class :
https://www.codeproject.com/Articles/24969/An-MFC-picture-control-to-dynamically-show-picture
Here is my code onDraw
void CPictureCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if (mBIsPicLoaded) {
RECT rc;
this->GetClientRect(&rc);
Graphics graphics(lpDrawItemStruct->hDC);
Image image(mPstream);
graphics.DrawImage(&image, (INT)rc.left, (INT)rc.top, (INT)(rc.right - rc.left), (INT)(rc.bottom - rc.top));
}
}
The image is displayed as a rectangle. But I would like to make it round.
How to do it ?
Thanks

Related

MFC GDI+ Text rendering issue

I'm currently using 5MP Camera, so I convert BYTE* to GDI+ Bitmap object and uses Graphics object to draw on picture control (all GDI+ objects)
and I want to draw a string on it and when I do so, resolution (quality or whatsoever) gets strange. here're the images.
this is the original image
this is the image with the text on it
And here's my code. it uses MFC's WM_MOUSEMOVE. and when mouse pointer gets on CRect(dispRC[array]), it renders string "aa" on the Bitmap object.
and when I do so, quality of image gets lower or I don't exactly know it changes the IMAGE. (You might not notice because those are captured images, but latter image's quality gets lower.)
void CSmall_StudioDlg::OnMouseMove(UINT nFlags, CPoint point)
{
CPoint insidePoint;
// MAXCAM is the number of bitmap objects.
for (int i = 0; i < MAXCAM; i++)
{
// m_pBitmap[MAXCAM] is array of Bitmap* which contains address of Gdiplus::Bitmap objects.
if (m_pBitmap[i] != NULL)
{
// m_rcDisp[MAXCAM] are CRect objects which has information of picture control.
// i.e. GetDlgItem(IDC_BIN_DISP)->GetWindowRect(m_rcDisp[BINARY_VID]);
if (point.x > m_rcDisp[i].TopLeft().x && point.y > m_rcDisp[i].TopLeft().y)
{
if (point.x < m_rcDisp[i].BottomRight().x && point.y < m_rcDisp[i].BottomRight().y)
{
StringFormat SF;
insidePoint.x = point.x - m_rcDisp[i].TopLeft().x;
insidePoint.y = point.y - m_rcDisp[i].TopLeft().y;
Graphics textG(m_pBitmap[i]);
textG.SetTextRenderingHint(TextRenderingHintSingleBitPerPixel);
Gdiplus::Font F(L"Palatino Linotype Bold", 10, FontStyleBold, UnitPixel);
RectF R(insidePoint.x, insidePoint.y, 20, 100);
SF.SetAlignment(StringAlignmentCenter);
SF.SetLineAlignment(StringAlignmentCenter);
SolidBrush B(Color(0, 0, 0));
textG.DrawString(_T("aa"), -1, &F, R, &SF, &B);
// m_pGraphics[MAXCAM] is made like this
// i.e.
// static CClientDC roiDc(GetDlgItem(IDC_ROI_DISP));
// m_hDC[ROI_VID] = roiDc.GetSafeHdc();
// m_pGraphics[ROI_VID] = Graphics::FromHDC(m_hDC[ROI_VID]);
m_pGraphics[i]->DrawImage(m_pBitmap[i], 0, 0, m_vidwidth[i], m_vidheight[i]);
}
}
}
}
CDialogEx::OnMouseMove(nFlags, point);
}
Hope I get a helpful answer.
Thanks!
I had a similar problem and solved it by creating the GDI+ font from a Windows font like this:
Gdiplus::Font font(hDc, hFont);
where hDc is a DC handle and hFont is a font handle.
Can you try if this helps?

How to display a image on a user defined rectangle with SDL_surface?

Im working on a project with my friend and we have run into an issue with surfaces and windows in SDL.
Currently we are able to create a window and display a rectangle on that window and move it around. The next thing we want to do is take a image and display it on a rectangle and then move it around the screen.
We started with taking the SDL_window* and turning it into SDL_surface* though this would take the image and display it on the background of the window.
Is there a way to turn a rectangle we create into a surface and display the image on that rectangle?
I have also tried using textures and it distorts the image when I tried to move it and the whole image doesn’t move with the rectangle.
// this happens in the constructor
temp_image_sur = IMG_Load( image_location.c_str() );
if( temp_image_sur == NULL )
{
std::cout << "Image could not be loaded" <<std::endl;
exit(1);
}
// This is in the actual draw function.
display_surface = SDL_GetWindowSurface( display_window );
if(display_surface == NULL )
{
printf(" null im exiting here %s\n", SDL_GetError());
exit(1);
}
image_surface = SDL_ConvertSurface( temp_image_sur, display_surface->format, 0 );
image_size = { this->location.x, this->location.y, this->size.width, this->size.height };
SDL_BlitSurface( image_surface, &image_size, display_surface, &image_size );
This is what we did for our first attempt, and the image was displaying on the base window. I believe I understand why it is displaying on the base window, it is because we are using that window as the surface, though I'm confused how do I make a user defined rectangle the surface?
We did try using SDL_CreateRGBSurface, though nothing is being displayed on the screen when we do this either.
display_surface = SDL_CreateRGBSurface(0, this->size.width, this->size.height, 1, this->color.red, this->color.green, this->color.blue, this->color.alpha);
Thanks guys!
Please let me know if there is anymore information you need, this is my first time posting and I tried to put all the info that I could think of.
Create a texture from your image surface by using SDL_CreateTextureFromSurface:
SDL_Texture* image_surface = SDL_CreateTextureFromSurface(renderer, temp_image_sur);
(remember to free it with SDL_DestroyTexture)
then use SDL_RenderCopy to draw it:
SDL_RenderCopy(renderer, image_texture, nullptr, &image_rect);
where image_rect is a SDL_Rect and the destination rectangle you want to draw your image to, for example:
SDL_rect image_rect = {10, 10, 200, 200};
To move your image simply change image_rect.x and/or image_rect.y

How to draw outside a a CButton

I have to draw left (not inside) from a CButton, two little red and green rectangle. I am not sure how to achieve this in the right way. Can I use in DrawItem() the device context from the parent DC to draw it?
void CSpecialButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{ ..
CDC* parentDC = GetParent()->GetDC();
CRect rcClrRed;
.. calculate rcClrRed;
parentDC->FillSolidRect(rcRed, RGB(255,0,0);
}

MFC: flickering issue with GDI+

I create a sample dialog application which has a circle drawn. Also on mouse move the circle will be re-drawn. I am providing my code below. Its also compilable.
I tried using double buffering and erasebackground, i was not getting the flickering issue, but i observed that the drawining is not erased properly. So to erase, in OnPaint i wrote the erasing code. Again i am facing the flickering issue.
void CPOCDlg::OnPaint()
{
CPaintDC dc(this);
GetClientRect(&clientRect);
circle = clientRect;
circle.DeflateRect(100,100);
dc.SelectStockObject(NULL_BRUSH);
dc.SelectStockObject(NULL_PEN);
dc.FillSolidRect(circle, ::GetSysColor(COLOR_BTNFACE));
Bitmap buffer(circle.right, circle.bottom);
Graphics graphicsbuf(&buffer);
Graphics graphics(dc.m_hDC);
graphicsbuf.SetSmoothingMode(SmoothingModeHighQuality);
SolidBrush brush(Color(255,71,71,71));
Pen bluePen(Color(255, 0, 0, 255),1);
graphicsbuf.DrawEllipse(&bluePen,Rect(circle.left,circle.top,circle.Width(),circle.Height()));
graphicsbuf.SetSmoothingMode(SmoothingModeHighQuality);
graphics.DrawImage(&buffer, 0, 0);
}
void CPOCDlg::OnMouseMove(UINT nFlags, CPoint point)
{
m_point = point;
InvalidateRect(circle,FALSE);
CDialogEx::OnMouseMove(nFlags, point);
}
BOOL CPOCDlg::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
Please let me know if i am doing any mistake.
You need to use so called double buffer technique to prevent flickering:
// create Mem DC
dcMemory = new CDC;
dcMemory->CreateCompatibleDC(pDC);
pDC->SetMapMode(MM_TEXT);
dcMemory->SetMapMode(MM_TEXT);
// TODO: draw to memDC here
//switch back to paint dc
pDC->BitBlt(rectDirty.left, rectDirty.top,
rectDirty.Width(), rectDirty.Height(),
dcMemory,
rectDirty.left,rectDirty.top,SRCCOPY);
dcMemory->DeleteDC();
delete dcMemory;
dcMemory = NULL;

Use a PNG as a background image in MFC window

I developed a small program in MFC that can display shapes and move them around.
I would like to be able to select PNG images that can be used as the background of the the main MFC window.
Currently, the background is set to black in the following code:
...
CBrush brush;
brush.CreateSolidBrush(RGB(0,0,0));
myCDC->FillRect(r,&brush);
...
I've found classes that might allow me to upload Bitmaps - for example [CStatic][1] - but have had no luck finding something for PNGs.
Does anyone know how to do this, or have you ever done something similar?
You can try to use the class Image of Gdiplus
http://msdn.microsoft.com/en-us/library/windows/desktop/ms534462(v=vs.85).aspx
because the png file can have the alpha property, but the bitmap does not have,
so I always use the Image to load the png file.
wchar_t szFile[256] = {0}; // png image file path
Image* m_pImage;
m_pImage = new Image(szFile, FALSE); // Load png file
// drawing png image
CPaintDC dc(this); // 用于绘制的设备上下文
Graphics graphics(dc.m_hDC);
if(m_pImage != NULL && (m_pImage->GetLastStatus() == Gdiplus::Status::Ok))
{
graphics.DrawImage(m_pImage, 0, 0, m_pImage->GetWidth(), m_pImage->GetHeight());
}
// need delete it, if you donot need it
if (m_pImage != NULL)
{
delete m_pImage;
m_pImage = NULL;
}
I think that GDI+ is the best and simple way for display images. It supports jpg, gif, tiff as well as png.
You can just display a image as below code,
void OnPaint(HDC hdc)
{
Graphics G(hdc);
Image I(L"snowman.jpg");
if (I.GetLastStatus() != Ok) {
MessageBox(hWndMain,TEXT("cannot read the image file"), TEXT("error"),MB_OK);
return;
}
G.DrawImage(&I,0,0);
}
Also, you can change image during execution time using FromFile static method.
Image *pI;
pI=Image::FromFile(L"snowman.jpg");
G.DrawImage(pI,0,0);
delete pI;