I am a newbie to MFC.
I am trying to copy a CScrollView dialog (which has drawn contents) into a image and then save that to a image as bmp or jpg or tiff.
I know how to save the CImage into a file, but i am unable to copy the draw area into a new CImage object. I know the procedure to copy. but i am unable to get in the code.
I need to get the CDC of the drawn area.
Create a new DC and copy that to new DC or an image.
Save the image.
Any pointers please?
You can create CImage based on Window size, and just draw on that image with BitBlt. Use CImage::GetDC() to obtain HDC, don't forget to call CImage::ReleaseDC() later.
CDC *pDC; //or use CClientDC, CPaintDC etc.
//do all drawings on pDC first
//pDC->TextOut(0,0,L"hello world")...
CRect rc;
GetClientRect(&rc);
CImage image;
image.Create(rc.Width(), rc.Height(), 32);
HDC imageHDC = image.GetDC();
::BitBlt(imageHDC, 0, 0, rc.Width(), rc.Height(), pDC->GetSafeHdc(), 0, 0, SRCCOPY);
image.Save(L"fileName.jpg", GUID_NULL);
image.ReleaseDC();
Related
I am new to FreeImage. I just want to read an JPEG image and display it in my MFC dialog. How do I do that? I try that using ImageStone by doing:
img.Load(blob.data, size, IMG_JPG);
img.Draw(hdc, DC);
Now, how do I do the same thing using FreeImage?
I tried and it worked exactly the way you described.... I used SHCreateMemStream and fed the stream to the overloaded LOAD method of CImage. Everything worked perfectly.
Thank you so much,
Makoto
CImage im;
IStream* is = SHCreateMemStream(Blob.pData, nSize);
HRESULT hr = im.Load(is);
RECT rect = { 0, 0, 500, 500 };
BOOL b = im.Draw(hdc, rect);
My ImageView is matching screen size on x-axis and is using remaining space on y-axis in my layout. I want to create bitmap into this ImageView with exactly the same size as the ImageView is. How to make it please? Can it be done by some automatic setting, should I call some measure function?
I tried SetAdjustViewBounds() but it didn't work for me.
Creating Bitmap big enough (I don't like much such a memory wasting) and setting SetScaleType(ImageView.ScaleType.Matrix) works, but still when I'm making drawing operations on canvas, I don't know real size of area I should paint into, both canvas and bitmap height are equal to yScreen while imgWeekView height is pretending to be 0, even though it paints whole desired area with gray color.
imgWeekView = new ImageView(context);
//imgWeekView.SetAdjustViewBounds(true);
imgWeekView.SetScaleType(ImageView.ScaleType.Matrix);
layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent,1f);
layoutParams.Height = 0;
imgWeekView.LayoutParameters = layoutParams;
Bitmap bitmap = Bitmap.CreateBitmap((int)xScreen, (int)yScreen, Bitmap.Config.Argb8888);
cnvWeekView = new Canvas(bitmap);
imgWeekView.SetImageBitmap(bitmap);
linearLayout.AddView(imgWeekView); //whole activity layout
//Test
cnvWeekView.DrawColor(new Color(128, 128, 128));
Paint paint = new Paint(PaintFlags.AntiAlias);
paint.Color = new Color(255, 255,0);
cnvWeekView.DrawCircle(50, 50, 40, paint);
Finally I found a way how to measure my ImageView and here I will post my answer.
I believed that there should be much easier solution, but maybe there isn't. From this question I took most of the important data:
How to get the width and height of an android.widget.ImageView?
Things look however a little different in my android application and I'm not experienced enough to tell why. I had to change things a little. I had to learn a bit about interfaces and this question helped too.
Implementing the View.IOnTouchListener interface
Here is how I combined things. First I created class that will do the measure.
public class MyPredrawListener : Java.Lang.Object, ViewTreeObserver.IOnPreDrawListener
{
ImageView imageView;
public MyPredrawListener(ImageView img)
{
imageView = img;
}
public bool OnPreDraw()
{
imageView.ViewTreeObserver.RemoveOnPreDrawListener(this);
int finalHeight = imageView.MeasuredHeight;
int finalWidth = imageView.MeasuredWidth;
Bitmap bitmap = Bitmap.CreateBitmap(finalWidth, finalHeight, Bitmap.Config.Argb8888);
imageView.SetImageBitmap(bitmap);
//Test to see result
Canvas cnv = new Canvas(bitmap);
Paint paint = new Paint();
paint.Color = new Color(255, 255, 0);
cnv.DrawColor(new Color(128, 128, 128));
cnv.DrawCircle(finalWidth-50, finalHeight-50, 50, paint);
return true;
}
}
And in code where I create my imageView I set the listener like this.
imgWeekView = new ImageView(context);
MyPredrawListener listener=new MyPredrawListener(imgWeekView);
imgWeekView.ViewTreeObserver.AddOnPreDrawListener(listener);
In OnPreDraw function I put test code to see the result graphically, clearing bitmap to gray color and painting yellow circle to bottom right of a view.
When taking a screenshot of my scene in JavaFx, I save the BufferedImage to a file as a PNG/JPG. When I try to maximize the image size to its full length, I get Black borders on the picture from the left of the image to the bottom without the image increasing its size at all.
The size of the image only increases until I set my dimensions to 1300x700 as shown below.
BufferedImage image = new BufferedImage(1300, 700, BufferedImage.TYPE_INT_RGB);
However once I increase the dimensions greater than 1300x700, the black borders appear.
The following picture is set to
BufferedImage image = new BufferedImage(1500, 900, BufferedImage.TYPE_INT_RGB);
As you can see, part of the image is still cut off and there is now a big black border next to the image rather than the actual full sized image.
The following picture is set to
BufferedImage image = new BufferedImage(1300, 700, BufferedImage.TYPE_INT_RGB);
As you can see, the image is still cut off at the same spot as before but there is no black border along side with it.
How can I fit the whole snapshot of my current scene into one file without these borders and without any of the content getting cut off?
Here is my code:
File fa = new File("test.jpg");
snapshot = quotes.getScene().snapshot(null);
RenderedImage renderedImage = SwingFXUtils.fromFXImage(snapshot, null);
BufferedImage image = new BufferedImage(1300, 700, BufferedImage.TYPE_INT_RGB);
image.setData(renderedImage.getData());
ImageIO.write(image, "jpg", fa);
The black border comes from uninitialized pixel buffer, inside your BufferedImage object.
So, I guess the renderedImage itself does not contains the right part of your scene.
The scene may not yet be properly resized when you are taking the snapshot. Try to give an appropriate WritableImage to the snapshot method:
snapshot = quotes.getScene().snapshot(new WritableImage(1500, 900));
In an iPhone app, I wrote some code to resize an image taken from the photo album, in order to use it as background for the app.
The code is strongly inspired from what I could find looking on the net. Namely here:
http://forrst.com/posts/UIImage_simple_resize_and_crop_image-sUG#comment-land
It works fine on the simulator. But it looks like no resizing at all is happening on the device. Why is that? Any idea?
Thank you for any tip.
hi Michel.
i dont know more about your code but i am using this code you can try
this you will find your solution.
here you need to pass image and the required width and height for
that image in this function and you will get another image with that
new size.
-(UIImage *)resizeImage:(UIImage *)image3 width:(int)width height:(int)height {
CGImageRef imageRef = [image3 CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
//if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), 4 * width, CGImageGetColorSpace(imageRef), alphaInfo);
CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *result = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return result;
}
I'm having an hard time finding how to draw/print a String in a canvas, rotated 90º, NOT vertical letters. After some different approaches without success I was trying to follow one that would involve printing the Graphics object to an Image object. As the API is reduced it has been a difficult task.
So basically what I'm asking you guys is if you know how to draw a String rotated 90º in a Canvas or if you don't, how can I save the graphics object to an Image one so I can follow my "hint".
Thank you very much!
Guilherme
Finally, one last research in the web and here it is:
//The text that will be displayed
String s="java";
//Create the blank image, specifying its size
Image img=Image.createImage(50,50);
//Create an instance of the image's Graphics class and draw the string to it
Graphics gr=img.getGraphics();
gr.drawString(s, 0, 0, Graphics.TOP|Graphics.LEFT);
//Display the image, specifying the rotation value. For example, 90 degrees
g.drawRegion(img, 0, 0, 50, 50, Sprite.TRANS_ROT90, 0, 0, Graphics.TOP|Graphics.LEFT);
from: http://wiki.forum.nokia.com/index.php/How_to_display_rotated_text_in_Java_ME
Thank you!