I would like to know if there's a way to display database value on an image, like I have a image template of a certificate and i would like to display name from the database, cause I have like 1000 names and there Image have to be made, is it possible from me to make a c# app for that. Im sorry new to this I did searches but i can't find anything.
Thanks
You can do it by the GDI+ drawing in .NET. first, load your template image, then draw a string to it, then save it as a new image. load all the names from database and process them in a loop, that will be ok. what you must focus on is to find the proper drawing position and beautify the font, my sample is below:
private void btnDrawImage_Click(object sender, EventArgs e)
{
string templateFile = Application.StartupPath + Path.DirectorySeparatorChar + "template_picture.jpg";
//customerize your dispaly string style
Font ft = new System.Drawing.Font("SimSun", 24);
Brush brush = Brushes.Red;
//start position(x,y)
Point startPt = new Point(100, 100);
//names from database
var nameList = new List<string>(){
"scott yang",
"Vivi",
"maxleaf",
"lenka"};
//process image on every name
foreach (string name in nameList)
{
string msg = "Welcome " + name;
Image templateImage = Image.FromFile(templateFile);
Graphics g = Graphics.FromImage(templateImage);
g.DrawString(msg, ft, brush, startPt.X, startPt.Y);
g.Dispose();
string savePath = "c:\\" + name + ".jpg";
templateImage.Save(savePath);
}
}
here is my result, I hope it is helpful to you.
Related
I want to load the image from the URL to the control.But it loads very slowly and When I clicked on tab 2 and returned to tab 1, the app was stalled.
enter image description here
public void ShowImageFromJsonURL()
{
string json = (new
WebClient()).DownloadString("http://ddragon.leagueoflegends.com/cdn/6.24.1/data/en_US/profileicon.json");
var result = JsonConvert.DeserializeObject<ProfileIcon>(json);
string version = result.Version.ToString();
string type = result.Type;
foreach (var item in result.Data)
{
string url = "http://ddragon.leagueoflegends.com/cdn/" + version + "/img/" + type + "/" + item.Value.Image.Full;
Image image = new Image()
{
Margin = new Thickness(10, 20, 0, 0),
Width = 150,
Height = 150,
Source = new BitmapImage(new Uri(url, UriKind.Absolute))
};
WrapPanelMain.Children.Add(image);
}
}
Apart from all your customers having a better internet connection, here are some options within your control:
A) Save the images with lower quality for quicker downloads.
B) Use a real CDN.
C) Cache the images in WPF app startup.
D) Use Async or a background worker to load the images to avoid the application freezing.
I have to save an image in post request in byte64String format
when i save that image i get A generic error occurred in GDI+
here is my code
byte[] ix = Convert.FromBase64String(obj.Image);
var ID = obj.Id;
using (var mStream = new MemoryStream(ix))
{
var img = Image.FromStream(mStream);
var image = obj.ImageName + ".jpg";
string path = HostingEnvironment.MapPath("/Images/" + ImageType + "/" + ID + "/" + image);
System.IO.Directory.CreateDirectory(path);
try
{
img.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception e)
{
var d = e;
}
}
also
this is not a permission issue as i am able to create text files in the same directory
Quite simply you are confusing paths and filenames.
The problem if could hazzard a guess, you probably have a folder that is your filename, and you are trying to save a file with that same name, which windows forbids
Your code tweaked
var image = $"{obj.ImageName }.jpg";
// get the path, and only the path
string path = HostingEnvironment.MapPath($"/Images/{ImageType}/{ID}/");
// Create directory if needed (from that path)
Directory.CreateDirectory(path,image);
...
// now create the correct full path
var fullPath = Path.Combine(path,fileName);
// save
img.Save(fullPath, ImageFormat.Jpeg);
I am working on an image which has the text on it.
Text example: X4 200 , X3 400, X8 2o and the same type of text present on image.
I need to read the X4 and corresponding value from C#,and then i need to save those values in a text file.
Please let me know how can i start with it.
I am trying with this code on button click event.
' OpenFileDialog ofd = new OpenFileDialog();
if(ofd.ShowDialog()==DialogResult.OK)
{
pictureBox1.Image =(Bitmap)Bitmap.FromFile(ofd.FileName);
}
using (var tEngine = new TesseractEngine("./tessdata", "eng", EngineMode.TesseractAndCube))
{
using (var img = Pix.LoadFromFile(ofd.FileName))
{
using (var page = tEngine.Process(img))
{
var text = page.GetText();
textBox1.Text = text;
}
}
'
image is here [1]: https://i.stack.imgur.com/PgcpZ.png
I'm trying to insert images in my rich text box in C#, but so far I'm only failing. Miserably.
This is the code that I am using:
Clipboard.SetImage(Image.FromFile(Application.StartupPath + #"\PIC\" + i + ".bmp"));
chat.Paste();
The real problem is I am not able to put both text and image in the textbox. The moment I insert text after copying the image the image disappears. I am unable to find a solution for this
Can anybody help me with this? Please???
Thanks
RichTextBox rtb = new RichTextBox();
byte[] headerImage = (byte[])(dr["ImageData"]);
string imageData = string.Empty;
if (headerImage != null && headerImage.Length > 0)
{
Bitmap bmp = new Bitmap(new MemoryStream(headerImage));
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Position = 0;
imageData = #"{\pict\jpegblip\picw10449\pich3280\picwgoal9924\pichgoal1860\ " + BitConverter.ToString(ms.ToArray()).Replace("-", string.Empty).ToLower() + "}";
ms.Dispose();
}
string finalrtfdata = rtb.Rtf;
finalrtfdata = finalrtfdata.Replace("&ImageHeader&", imageData);
// finalrtfdata contain your image data along with rtf tags.
Try this out,you can paste this into your code and call it:
place a picture into your project to embeded resource,and call this method
passing the richtextbox.
private void createImage(Control item)
{
Hashtable image = new Hashtable(1);
image.Add(item,yourproject.Properties.Resources.yourpicturename);
Clipboard.SetImage((Image)image[item]);
((RichTextBox)item).Paste();
}
private static void createImage(RichTextBox item)
{
var image = new Hashtable(1) { { item, Properties.Resources.yourimage } };
Clipboard.SetImage((Image)image[item]);
item.Paste();
}
I am trying to load pictures with the code below:
void LoadPictures(int s)
{
grdScene.Children.Clear();
TabControl tab = new TabControl();
for (int i = s; i <= s+3; i++)
{
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap("C:\\img\\" + i + ".png");
TabItem tabItem = new TabItem();
tabItem.Header = "Scene " + i;
var bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
Image image = new Image();
image.Source = bitmapSource;
tabItem.Content = image;
tab.Items.Add(tabItem);
}
grdScene.Children.Add(tab);//grdScene is Grid
}
it works fine but every time I run this method the memory is going up.
Then I tried to set all the items to null, but no success. Here is the code:
foreach (var child in grdScene.Children)
{
TabControl tab1 = (TabControl)child;
foreach (TabItem item in tab1.Items)
{
Image img = (Image)item.Content;
img = null;
}
tab1 = null;
}
I tried GC.Collect() but nothing changes. What should I do to solve this problem?
Why are you doing this via code-behind?
From the look of your code the tab items are populated entirely by the image.
You could just bind a tabcontrol to the image path collection and use a style to describe the look you're after, it'd be a lot simpler and the WPF core would then be taking care of the image loading and memory management itself.
If that's not usable for you then your issue is coming from how you're loading the image, you're doing it via an unsafe interop method that doesn't release the memory for you, you have to dispose of the bitmap youreself .Dispose()
You'd be better doing: loading the source of a BitmapImage in WPF?
Using BitmapImage is likely the better way to go if you want to remain doing it in the code-behind rather than the Interop methods.
SO:
void LoadPictures(int s)
{
grdScene.Children.Clear();
TabControl tab = new TabControl();
for (int i = s; i <= s + 3; i++)
{
BitmapImage bmp = new BitmapImage(new Uri("C:\\img\\" + i + ".png"));
TabItem tabItem = new TabItem();
tabItem.Header = "Scene " + i;
tabItem.Content = new Image(){Source = bmp};
tab.Items.Add(tabItem);
}
grdScene.Children.Add(tab);
}
Should do the trick.
Try to call Dispose on all IDisposables, such as System.Drawing.Bitmap or better use using..
using(var bmp = new System.Drawing.Bitmap("C:\\img\\" + i + ".png"))
{
.....
}