I have to render the text from a TextBox into a WriteableBitmap.
This code is working pretty well but the text in it is blurry or anti-aliased.
Any ideas on how to keep it aliased and crisp?
text = new FormattedText(tb.Text,
new CultureInfo("de-de"),
FlowDirection.LeftToRight,
new Typeface(tb.FontFamily, FontStyles.Normal, tb.FontWeight, new FontStretch()),
tb.FontSize,
tb.Foreground);
drawingContext = dv.RenderOpen();
drawingContext.DrawText(text, Pos);
drawingContext.Close();
rtb.Render(dv);
rtb.CopyPixels(new Int32Rect(0, 0, rtb.PixelWidth, rtb.PixelHeight), wb.BackBuffer, wb.BackBufferStride * wb.PixelHeight, wb.BackBufferStride);
What I found did not work:
TextOptions.SetTextFormattingMode(dv, TextFormattingMode.Display);
TextOptions.SetTextRenderingMode(dv, TextRenderingMode.Aliased);
or
RenderOptions.SetBitmapScalingMode(dv, BitmapScalingMode.NearestNeighbor);
RenderOptions.SetEdgeMode(dv, EdgeMode.Aliased);
Related
I'm trying to capture whole desktop screen (front buffer) and add a logo and caption to each frame.
I load the logo (.png or .jpeg file) as IDirect3DTexture9 and I try to add it to a IDirect3DSurface9 image frame (screenshot).
As I'm new to DirecX9, I have no idea how to copy the logo (texture) to the screenshot (surface/buffer). Any help would be appreciated.
(If there's any other way to add logo to each frame without involving texture, please do tell me.)
EDIT:
I have used the code suggested in an answer below. The hr result returned is an error.
IDirect3DSurface9 *pSurface = NULL;
pDevice->GetFrontBufferData(0, pSurface); //my screenshot
LPDIRECT3DTEXTURE9 tex = NULL; //my logo
//[code to load logo from file here]
IDirect3DSurface9 *pSurf = NULL;
tex->GetSurfaceLevel(0, &pSurf);
hr = pDevice->StretchRect(pSurf, NULL, pSurface, NULL, D3DTEXF_NONE); //HR GIVES AN ERROR
You can use StretchRect.
The code will look something like this (pseudo):
ID3DSurface9 *pScreenSurface = ... // your screenshot surface should be created in default pool and the same format as your texture(see bellow), like this:
CreateOffscreenPlainSurface(width, height, D3DFMT_X8B8G8R8, D3DPOOL_DEFAULT, &pScreenSurface, NULL);
// Now get a screenshot by using either GetFrontBufferData, or GetBackBuffer/GetRenderTargetData by supplying pScreenSurface as a parameter (not shown here).
ID3DTexture9 *pTexture = ... // your texture should be loaded in default pool and the same format as your screenshot surface, like this:
D3DXCreateTextureFromFileEx(*ppDevice, L"icon.bmp", 40, 40, D3DX_DEFAULT, 0,
D3DFMT_X8B8G8R8, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL,
NULL, &pTexture);
ID3DSurface9 *pSurf;
pTexture->GetSurfaceLevel(0, &pSurf); // don't forget to Release pSurf after StretchRect
RECT rc = { ... initialize the destination rectangle here };
pDevice->StretchRect(pSurf, NULL, pScreenSurface, &rc);
You need to specify a destination rectangle inside the destination surface, where you want your texture to be copied.
I am trying to create a notification Icon based application in which I want to display some numbers ranging from 1-999.
I looked at this video which is similar to what I want to do but here the system tray icon just displays the icon and it shows a pop up rather than the system tray icon showing the number or any text.
Excluding the popup item, all I want to do is to read a number (input from somewhere) and display that number in the notification icon section.
I am open to trying any technology (QT, .net) for doing this. Basically, I am looking for some examples.
While parts of your question are vague, this is very possible, I'd even dare-say quite simple. Since you mentioned you're open to trying any technology, C# would probably simplify things for you.
Generate a new 16 x 16 Bitmap and draw the number to it using the Graphics class.
Convert the Image instance to an Icon instance, after disposing of your Graphics object.
Set the Icon property of your NotifyIcon to the icon you've just created.
These are the basic steps. You'll likely need to do some research if you aren't familiar with the classes used.
Thanks for replying to my question. Here is what I came up with. Not sure if this is what you were talking about.
Bitmap bmp = new Bitmap(WindowsFormsApplication2.Properties.Resources._16by16BitmapIcon);
RectangleF rectf = new RectangleF(2, 2, 16, 16);
Graphics g = Graphics.FromImage(bmp);
g.DrawString("99", new Font("Tahoma", 7), Brushes.Blue, rectf);
pictureBox1.Image = bmp;
pictureBox1.Height = bmp.Height;
pictureBox1.Width = bmp.Width;
g.Dispose();
var thumb = (Bitmap)bmp.GetThumbnailImage(64, 64, null, IntPtr.Zero);
thumb.MakeTransparent();
notifyIcon1.Icon = Icon.FromHandle(thumb.GetHicon());
Now my next question could this be done in a better way? This is my first C Sharp app so any suggestions are welcome!
public void ShowText(string text, Font font, Color col)
{
Brush brush = new SolidBrush(col);
// Create a bitmap and draw text on it
Bitmap bitmap = new Bitmap(16, 16);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.DrawString(text, font, brush, 0, 0);
// Convert the bitmap with text to an Icon
Icon icon = Icon.FromHandle(bitmap.GetHicon());
m_notifyIcon.Icon = icon;
}
I need a MenuBar on the left side of my screen that can slide in and out. As this app is crossplattform it should be on the left side and not like the usual ApplicationBar on the Bottom or NavigationBar on Top.
What I have so far is:
private bool isTapped = false;
private void TEST_TAP(object sender, TappedRoutedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("TAP");
Storyboard s = new Storyboard();
DoubleAnimation doubleAni = new DoubleAnimation();
doubleAni.To = -30;
if (isTapped) doubleAni.To = 0;
doubleAni.Duration = new Duration(TimeSpan.FromMilliseconds(500));
Storyboard.SetTarget(doubleAni, LOGO);
Storyboard.SetTargetProperty(doubleAni, "(UIElement.RenderTransform).(TranslateTransform.XProperty)");
s.Children.Add(doubleAni);
s.Begin();
}
The bar I want to move is a Grid or should be something similar. But this even fails to move a single Image. I googled a bit and replaced "(UIElement.RenderTransform).(TranslateTransform.XProperty)" with "(Canvas.Left)". This doesn't crash but also doesn't move anything else.
Any ideas or solutions?
EDIT:
Well I played around a lot and now I know (and tested it) that "(Canvas.Left)" only works inside of a Canvas. Yay.
My App looks like this: Scrollview (horizontal) and inside is a Grid with columns to display different stuff. The left-most Column contains another Grid that I want to move out of the screen and back in again. But how do I move a grid with Storyboard-animation?
If anybody thinks of a different way to do this, I'm totally happy if it works in any way.
EDIT2:
It seems to be THE way to use Storyboard.SetTargetProperty(doubleAni, "(UIElement.RenderTransform).(CompositeTransform.TranslateX)"); but it keeps crashing =/
Before using this transform you need to add it:
Storyboard s = new Storyboard();
DoubleAnimation doubleAni = new DoubleAnimation();
doubleAni.To = to;
doubleAni.From = from;
doubleAni.Duration = new Duration(TimeSpan.FromMilliseconds(250));
// THIS LINE IS NEW
NaviBar.RenderTransform = new CompositeTransform();
Storyboard.SetTarget(doubleAni, NaviBar);
Storyboard.SetTargetProperty(doubleAni, "(UIElement.RenderTransform).(CompositeTransform.TranslateX)");
s.Children.Add(doubleAni);
s.Begin();
This does the trick. It's still not the functionality that I wanted but the hardest part - the animation - works now.
So in short, my issue is that the background image for my Live Tile is not being updated.
I know that the correct image is in Isolated Storage, and I also know that it is being set as the image for the tile, however only the tile sizes not currently being viewed update. For the tile size currently being used to update, the user has to cycle through the tile sizes several times. I assume the issue is something to do with a tile cache being constructed, and not easily being cleared. However, I have tried a few different methods to clear it - clearing all properties on the tile, setting unique filenames for each icon, and none have worked - I'm still forced to either cycle through the tiles or unpin and repin for the icon to update.
Are there any solutions or workarounds for this to force an update?
EDIT: The code I am using is:
Canvas canvasIcon = DrawCanvas(100, 172, 30, 30, brushWhite, brushWhite, PenLineCap.Square, 10);
WriteableBitmap wbIcon = new WriteableBitmap(canvasIcon, null);
wbIcon.Invalidate();
var stream = new IsolatedStorageFileStream("Shared/ShellContent/icon.png", FileMode.OpenOrCreate, isoStore);
wbIcon.WritePNG(stream);
stream.Close();
IconicTileData iconicTile = new IconicTileData() {
SmallIconImage = new Uri("isostore:/Shared/ShellContent/smallicon.png", UriKind.Absolute),
Title = "Placeholder",
IconImage = new Uri("isostore:/Shared/ShellContent/icon.png", UriKind.Absolute)
};
ShellTile tile = ShellTile.ActiveTiles.First();
tile.Update(iconicTile);
So, here is my problem. I have a JPanel, with a JLabel on it, and what I want is once that JLabel is clicked, it should resize, (ideally it will change with it's scale, but for now I am using a constant value). I have returned the image, and I now that I am able to scale it, but I just cant manage to make the original JLabel become the newly sized one.
So this is what should happen ideally, e.getSource should become the newly increased in size JLabel.
I know I'm pretty close, I did a JOptionPane as a debug statement to see if I can increase the size, and I can.
Why cant ((JLabel)me.getSource = a;
where a is my new JLabel?
Anyways, here is my code:
Please help me out.
public void mousePressed(MouseEvent me) {
//GreetingCard.setBackground.findComponentAt(me.getX(), me.getY());
//GreetingCard.setBackground.findComponentAt(me.getX(), me.getY)
JLabel a= (JLabel) me.getSource();
Icon icon = a.getIcon();
int scale = 4;
BufferedImage bi = new BufferedImage(
scale*icon.getIconWidth(),
scale*icon.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.scale(scale,scale);
icon.paintIcon(null,g,0,0);
g.dispose();
JLabel temp = new JLabel(new ImageIcon(bi));
((JLabel)me.getSource())= temp;
JOptionPane.showMessageDialog(
null,
new JLabel(new ImageIcon(bi)));
System.out.println("The size of the image is" + b.getIconWidth());
initiateEvent = me;
me.consume();
}
me.getSource() returns a JLabel, it doesn´t return a variable you can assign a new value.
Probably the best way is not to create a new JLabel but just assign the new ImageIcon to the old JLabel.
((JLabel)me.getSource()).setIcon(new ImageIcon(bi));
You also need to call label or panel updateUI() to force a "hard" repaint with resizing.