Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i worked on a project that in a step we must find a first character's position in a first line of a texts that we store it in a emf file .
also we have a another problem that , some times our text line may have a Equation and this Equation may have a anormal height . this means that , the lines top Distance from top of image (y=0) is Floating :(

for this reasons that I explain them for you , we must find a first characters position and also we must do this on a big range (like 1000 in a minute) and converting of emf file to bitmap is not good for our programs performance ..

any body can help me ?
Posted
Comments
Sergey Alexandrovich Kryukov 16-Sep-11 17:50pm    
Why you even ask about conversion to bitmap and consider performance? In a bitmap, after conversion, there are no any text anymore...
--SA

This is quite possible but not quite trivial. EMF file is not exactly a container of vector graphical primitive. It is rather a sequence of events and can be "played", much like a video, to get displayed. You can find the EMF's elements by using the method System.Drawing.Imaging.Metafile.PlayRecord. See http://msdn.microsoft.com/en-us/library/system.drawing.imaging.metafile.playrecord.aspx[^].

[EDIT]
Answering a follow-up question:

There are no pixels in EMF; they are created only when EMF is rendered in screen. The result also depends on the scale you render a metafile — it does not really have its own size.

You need to obtain a bitmap to calculate anything by pixels. You can create a bitmap, obtain an instance of System.Drawing.Graphics using System.Drawing.Graphics.FromImage. Then, you need to play a metafile on this instance and obtain a pixel graphics in the bitmap. To find pixels, never use GetPixel. Always use System.Drawing.Bitmap.LockBits, see the code sample here: http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx.

See:
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx[^].

—SA
 
Share this answer
 
v2
Comments
merkousha 17-Sep-11 1:38am    
dear SAKrykov ,
Thanks for your tracking .
I dont want to find any text .. I want to find the first black pixel in the right-top side of my vector (.emf) file .

how can I find the first black point in right-top side of my vector file . my .emf file is a black text in the White background .
Sergey Alexandrovich Kryukov 18-Sep-11 14:03pm    
There are no pixels in EMF. The pixels are created only when you render EMF on the bitmap surface.
You can render it by playing EMF on the bitmap and looking at the bitmap.
--SA
Sergey Alexandrovich Kryukov 18-Sep-11 14:23pm    
See my updated solution, after [EDIT].
To me, the whole idea of looking for a pixel for vector graphics looks weird. Are you sure you really need it. What for? Layout? You don't need it? Alignment of rendered vector graphics with pixel graphics? It would not make sense... If you shared your ultimate goals, you could get better advice. What is all this activity for, what's the idea?
--SA
merkousha 19-Sep-11 1:41am    
Thank you SAKrykov ,

This Code is my solution ,
It's opened an ".emf" file and saved needed section of it in a new bitmap item .
And process bitmap to find goal pixel !
Read it , and give me an idea , how I can do this , without converting vector file to bitmap . (for reducing process time)
Or have you any other idea about reducing this process's time ?
I think that my Code explains clearly about my goals.



My new optimized code is :



<pre lang="c#"> int maxx,maxy,j,i=1;
j = 0;
for (i = 1; i <= 100; i++)
{
Metafile metafile1 = new Metafile(@"e:\pix\emf\sample - Copy (" + i.ToString() + ").emf");
Bitmap newBitmap = new Bitmap(metafile1.Width / 40, metafile1.Height / 20);
newBitmap.SetResolution(300.0F, 300.0F);
Graphics myGraphics = Graphics.FromImage(newBitmap);
myGraphics.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, metafile1.Width / 20, metafile1.Height / 20));
Rectangle rcSrc = new Rectangle(metafile1.Width / 2, 0, metafile1.Width , metafile1.Height / 2);
Rectangle rcDest = new Rectangle(0, 0, metafile1.Width / 20, metafile1.Height / 20);
myGraphics.DrawImage(metafile1, rcDest, rcSrc, GraphicsUnit.Pixel);
newBitmap.Save(@"e:\pix\bmp\sample - Copy (" + i.ToString() + ").bmp");
maxx = 0;
maxy = newBitmap.Height;

int i1,j1;
for (i1 = newBitmap.Width-1; i1 >= newBitmap.Width - 20; i1--)
for (j1 = 1; j1 <= 30; j1++) {
Color pixelColor = newBitmap.GetPixel(i1, j1);
if (pixelColor.Name != "ffffffff") {
if (i1 > maxx || i1>=maxx && j1 < maxy)
{
maxx = i1;
maxy = j1;
}
}
}

}
MessageBox.Show("Converting is finished !!!");

</pre>
It seems you are trying to get the top left pixel of text?

If you just have text and everything else is transparent you can just load the Metafile and call .GetBounds(Imaging.MetafileFrameUnit.Pixel) on it!

Kris
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900