How can i resize image in html-helper tagbuilder - html-helper

The following C# codes creates image tag
which fetches image from a directory;
public static IHtmlString Image(this HtmlHelper helper, string src, string alt, int whichpic,string width,string height)
{
switch (whichpic)
{
case 1:
src = "~/assets/img/products/" + src + "_1.jpg";
break;
case 2:
src = "~/assets/img/products/" + src + "_2.jpg";
break;
case 3:
src = "~/assets/img/products/" + src + "_3.jpg";
break;
}
TagBuilder tb = new TagBuilder("img");
tb.Attributes.Add("src", VirtualPathUtility.ToAbsolute(src));
tb.Attributes.Add("alt",alt);
tb.Attributes.Add("width", width);
tb.Attributes.Add("height", height);
return new MvcHtmlString(tb.ToString(TagRenderMode.SelfClosing));
}
The problem is that, the width and height attribute doesn't work.
Can anyone help me to create width and height styles attribute?
Thank you

Related

Avoid window opening between two screens

I am writing a Photoshop plugin for Windows and want to place the plugin dialog in the center of the main window.
This is my code:
void centre_window(HWND hwnd){
RECT rs, rd;
HWND hw = GetParent(hwnd); // GetDesktopWindow();
if (GetWindowRect(hw, &rs) && GetWindowRect(hwnd, &rd))
MoveWindow(hwnd,(rs.right + rs.left + rd.left - rd.right) / 2,
(rs.bottom + rs.top + rd.top - rd.bottom) / 3,
rd.right - rd.left, rd.bottom - rd.top, TRUE);
}
So far, it works. But there is one flaw: If the main window is spread across two screens, then my window is between both screens.
I looked at other Photoshop plugins and they handle it like this:
Place the window at the main window center
If it would be between two screens, choose one of these two and place it at the border of the screen
How can I do that?
With the helpful hint of #IInspectable , I wrote following code:
void _doMonitorAdjustments(LPRECT rcPlugin) {
RECT rcMonitorWork;
HMONITOR hMonitor;
MONITORINFO grMonitorInfo = { sizeof(grMonitorInfo) };
int nXAdjust = 0, nYAdjust = 0, nPluginWidth, nPluginHeight, nMonitorWorkWidth, nMonitorWorkHeight;
hMonitor = MonitorFromRect(rcPlugin, MONITOR_DEFAULTTONEAREST);
if (hMonitor == NULL) return;
if (!GetMonitorInfoA(hMonitor, &grMonitorInfo)) return;
rcMonitorWork = grMonitorInfo.rcWork;
// Don't let the window exit the left/right borders of the monitor
nPluginWidth = rcPlugin->right - rcPlugin->left;
nMonitorWorkWidth = rcMonitorWork.right - rcMonitorWork.left;
if (nPluginWidth > nMonitorWorkWidth) {
// Window larger than screen width. Decrease the width!
rcPlugin->left = rcMonitorWork.left;
rcPlugin->right = rcMonitorWork.right;
}
else if (rcPlugin->left < rcMonitorWork.left) {
nXAdjust = rcMonitorWork.left - rcPlugin->left;
}
else if (rcPlugin->right > rcMonitorWork.right) {
nXAdjust = rcMonitorWork.right - rcPlugin->right;
}
// Don't let the window exit the top/bottom borders of the monitor
nPluginHeight = rcPlugin->bottom - rcPlugin->top;
nMonitorWorkHeight = rcMonitorWork.bottom - rcMonitorWork.top;
if (nPluginHeight > nMonitorWorkHeight) {
// Window larger than screen height. Decrease the height!
rcPlugin->top = rcMonitorWork.top;
rcPlugin->bottom = rcMonitorWork.bottom;
}
else if (rcPlugin->top < rcMonitorWork.top) {
nYAdjust = rcMonitorWork.top - rcPlugin->top;
}
else if (rcPlugin->bottom > rcMonitorWork.bottom) {
nYAdjust = rcMonitorWork.bottom - rcPlugin->bottom;
}
OffsetRect(rcPlugin, nXAdjust, nYAdjust);
}
/*
* Centers a window to the center of its parent form but avoids
* being spread across two screens.
*/
void centre_window(HWND hwnd) {
RECT rcParent, rcWindowOriginal, rcPlugin;
HWND hParent;
hParent = GetParent(hwnd);
if (hParent == NULL) hParent = GetDesktopWindow();
if (!GetWindowRect(hParent, &rcParent)) return;
if (!GetWindowRect(hwnd, &rcWindowOriginal)) return;
rcPlugin.left =
rcParent.left
+ (rcParent.right - rcParent.left) / 2
- (rcWindowOriginal.right - rcWindowOriginal.left) / 2;
rcPlugin.top =
rcParent.top
+ (rcParent.bottom - rcParent.top) / 2
- (rcWindowOriginal.bottom - rcWindowOriginal.top) / 2;
rcPlugin.right =
rcPlugin.left + rcWindowOriginal.right - rcWindowOriginal.left;
rcPlugin.bottom =
rcPlugin.top + rcWindowOriginal.bottom - rcWindowOriginal.top;
// Avoid that the window is spread across two screens
_doMonitorAdjustments(&rcPlugin);
MoveWindow(hwnd,
rcPlugin.left,
rcPlugin.top,
/*width=*/rcPlugin.right - rcPlugin.left,
/*height=*/rcPlugin.bottom - rcPlugin.top,
TRUE);
}

drag no border cef winform

Problem
I need to resize no border form,But when ChromiumWebBrowser's dock is fill, CEF will eat mouse related information, dragging the form becomes very insensitive.
I observed Google Chrome, when the mouse outside the form can be dragged to change the size of the form, how can I achieve the same effect as Google Chrome.
Update
I need drag to change form's size,and Draggable borderless window in CefSharp a lot of private code, I can't know how to implement it.for example, ChromeWidgetMessageInterceptor.SetupLoop,in the cefsharp source code, ChromeWidgetMessageInterceptor does not have SetupLoop.
Update2 Add the screenshot and the code
const int WM_MOUSEMOVE = 0x0200;
const int WM_MOUSELEAVE = 0x02A3;
const int WM_LBUTTONDOWN = 0x0201;
//
switch (message.Msg)
{
case WM_MOUSEMOVE:
var clientCursorPos = GetMousePoint(MousePosition);
var newE = new MouseEventArgs(MouseButtons.None, 0, clientCursorPos.X, clientCursorPos.Y, 0);
this.InvokeOnParent(delegate { this.OnMouseMove(newE); });
break;
case WM_LBUTTONDOWN:
if (Cursor != Cursors.Default)
{
//this.InvokeOnParent(delegate { this.ResizeForm(this.ResizeDir); });
this.InvokeOnParent(delegate
{
if (DesignMode) return;
var dir = -1;
switch (this.ResizeDir)
{
case ResizeDirection.BottomLeft:
dir = HTBOTTOMLEFT;
break;
case ResizeDirection.Left:
dir = HTLEFT;
break;
case ResizeDirection.Right:
dir = HTRIGHT;
break;
case ResizeDirection.BottomRight:
dir = HTBOTTOMRIGHT;
break;
case ResizeDirection.Bottom:
dir = HTBOTTOM;
break;
}
ReleaseCapture();
if (dir != -1)
{
SendMessage(Handle, WM_NCLBUTTONDOWN, dir, 0);
}
});
}
break;
case WM_MOUSELEAVE:
Console.WriteLine("WM_MOUSELEAVE");
break;
}
Mouse drag to change the size of the problem

C# Windows Application Image Resizing Before Saving

I have this Code to Save a Pic to my Solution Explorer Images Folder.
private void btnUploadImage_Click(object sender, EventArgs e)
{
//The String used to store the location of the file that is currently loaded in the picture box picFile
String location;
//The String used to store the name of the file that is currently loaded in the picture box picFile
String fileName;
ofdImageUpload.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
//Showing the fileopen dialog box
ofdImageUpload.ShowDialog();
//showing the image opened in the picturebox
imgCapture.Image = new Bitmap(ofdImageUpload.FileName);
//storing the location of the pic in variable
location = ofdImageUpload.FileName;
txtImgLocation.Text = location;
//storing the filename of the pic in variable
fileName = ofdImageUpload.SafeFileName;
//pictureboxImage.Image.Save();
imgCapture.SizeMode = PictureBoxSizeMode.StretchImage;
if (imgCapture.Image != null)
{
lblHiddenMsg.Text = "";
}
}
private void InsertGatepassEntry(int RowId)
{
string ContName = txtContName.Text.Trim();
string ContAdd = richtxtContAddress.Text.Trim();
string VisitorName = txtEmpName.Text.Trim();
string VisitorAdd = txtEmpAddress.Text.Trim();
string VisitorFathersName = txtEmpFatherName.Text.Trim();
string VisitorAge = txtEmpAge.Text.Trim();
string VisitorEsi = txtEsi.Text.Trim();
string VisitorContact = txtEmpContactNo.Text.Trim();
string VisitorBloodGrp = comboxBloodGroup.SelectedText.Trim();
string VisitorIssueDate = dtpEmpDateOfIssue.Text.Trim();
string imagename = ofdImageUpload.SafeFileName;
if (imagename != null || imagename != "") //Check If image is Selected from Computer's Hard Drive
{
if (imgCapture.Image != null)
{
//string imagepath = ofdImageUpload.FileName;
//string picname = imagepath.Substring(imagepath.LastIndexOf('\\'));
string picname = imagename;
string path = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("bin"));
Bitmap imgImage = new Bitmap(imgCapture.Image); //Create an object of Bitmap class/
//string fullPathName = path + "Images" + picname;
imgImage.Save(path + "Images\\" + txtEmpName.Text + txtEsi.Text + ".jpg");
string Image = "Images\\" + txtEmpName.Text + txtEsi.Text + ".jpg";
string GatepassNo = LoadLastGatepassNo();
switch (Contractor.InsertGatepassEntry(RowId, ContName, ContAdd, VisitorName, VisitorAdd, VisitorFathersName, VisitorAge, VisitorEsi, VisitorContact, VisitorBloodGrp, VisitorIssueDate, Image, GatepassNo))
{
case ProjectCreateStatus.Insertrow:
lblMessage.Text = "Information inserted successfully!";
lblMessage.ForeColor = System.Drawing.Color.Green;
lblGatepassNo.Text = GatepassNo;
break;
}
}
else
{
lblHiddenMsg.Visible = true;
lblHiddenMsg.Text = "Please capture or browse an image First";
}
}
else //image is directly uploding from Webcam Capture
{
string Image = lblHiddenMsg.Text;
string GatepassNo = LoadLastGatepassNo();
switch (Contractor.InsertGatepassEntry(RowId, ContName, ContAdd, VisitorName, VisitorAdd, VisitorFathersName, VisitorAge, VisitorEsi, VisitorContact, VisitorBloodGrp, VisitorIssueDate, Image, GatepassNo))
{
case ProjectCreateStatus.Insertrow:
lblMessage.Text = "Information inserted successfully!";
lblMessage.ForeColor = System.Drawing.Color.Green;
lblGatepassNo.Text = GatepassNo;
break;
}
}
}
private void bntCapture_Click(object sender, EventArgs e)
{
imgCapture.Image = imgVideo.Image;
Bitmap b = new Bitmap(imgCapture.Image);
string path = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("bin"));
b.Save(path + "Images\\" + txtEmpName.Text + txtEsi.Text + ".jpg");
lblHiddenMsg.Text = path + "Images\\" + txtEmpName.Text + txtEsi.Text + ".jpg";
}
Now I want to Save those uploaded Pic to my Images Folder to a particular Size like 250x250. can Anyone Help ? m new in Windows Application C#.
To resize your image use:
public static Image resizeImage(Image imgToResize, Size size)
{
return (Image)(new Bitmap(imgToResize, size));
}
yourImage = resizeImage(yourImage, new Size(250,250));
hi you can add the "Size" class as a parameter of your Bitmap constructor to resize the image
i did for you this function to make it easy hope that will help you
public static Image resizeImage(Image imgToResize, Size size)
{
return (Image)(new Bitmap(imgToResize, size));
}
yourImage = resizeImage(yourImage, new Size(250,250));

UISwipeGestureRecogniser not being recognised

I have a UIScrollView into which I am creating and adding UILabels containing the results of a simple LINQ query.
I have set up a UISwipeGestureRecognizer within the loop that generates the labels (iOS: issues with UIGestureRecognisers vs Subviews says I need a new recogniser for each control - I'm assuming I can do with a UILabel the same as UIImageView for adding a recogniser) and then added the recogniser to the label.
When the view containing the UIScrollView is started, the scrollview works as expected, but the swipe isn't.
Caveat : only tried this on the simulator, my iPhone is acting up.
private void CreateViewHistory()
{
float xtext = 4f;
float y = 4f;
int c = 0;
var tv = tank.Events.OrderByDescending(t => t.Date).ToList();
tbiHistClearAll.Enabled = enableDelete;
//tgrRemove.AddTarget(this, new Selector("screenSwipe"));
//pgrRemove.DelaysTouchesBegan = true;
foreach (var e in tv)
{
var tgrRemove = new UISwipeGestureRecognizer()
{
NumberOfTouchesRequired = 1,
Direction = UISwipeGestureRecognizerDirection.Right,
};
tgrRemove.AddTarget(this, new Selector("screenSwipe"));
svHistoryEvents.PanGestureRecognizer.RequireGestureRecognizerToFail(tgrRemove);
string info = string.Format("{0} - {1}{2} ({3})\n{4} - {5}\n{6} - {7}\n{8} - {9}", e.EventType, e.Volume, AppDelegate.Self.db.getShortVolumeUnits(), e.Date.ToShortDateString(),
StringUtils.GetString("Sowcrop.Implement"), e.Implement != null ? e.Implement.ImplementType : StringUtils.GetString("Common.NonRecorded"),
StringUtils.GetString("Common.User"), StringUtils.GetString("Common.NonRecorded"),
StringUtils.GetString("Common.Notes"), !string.IsNullOrEmpty(e.Notes) ? e.Notes : StringUtils.GetString("Common.NonRecorded"));
var lbl = new UILabel()
{
UserInteractionEnabled = true
};
lbl = UICreation.MakeLabelWithTag(svHistoryEvents, new RectangleF(xtext, y, 320f, 90f), info, UITextAlignment.Left, UIColor.Black, false, 4, c);
lbl.AddGestureRecognizer(tgrRemove);
svHistoryEvents.AddSubview(lbl);
lblTemp.Add(lbl);
c++;
y += 94f;
}
UIUtils.ResizeScrollView(svHistoryEvents);
}
[Export("screenSwipe")]
public void SwipeRemove(UIGestureRecognizer s)
{
var swipe = s as UIGestureRecognizer;
var tv = tank.Events.OrderByDescending(t => t.Date).ToList();
var txt = swipe.View as UILabel;
switch (swipe.State)
{
case UIGestureRecognizerState.Began:
Console.WriteLine("Swipe began");
break;
case UIGestureRecognizerState.Changed:
Console.WriteLine("Swipe changed");
tv.RemoveAt(txt.Tag);
CreateViewHistory();
break;
case UIGestureRecognizerState.Ended:
Console.WriteLine("Swipe ended");
break;
case UIGestureRecognizerState.Cancelled:
Console.WriteLine("Swipe cancelled");
break;
}
}
MakeLabelWithTag generates a UILabel which can then be added to the scrollview.
Am I missing something here, or do I need to do something special as the label is held within a scrollview?
I've also tried what has been suggested at UISwipeGestureRecogniser in a UIScrollView, but still without success.
Found the problem and it's probably the dumbest thing I've encountered in a long time!
To get the swipe gesture to work within a scrollview, you have to first encompass whatever it is you want to add within a UIView and then add that to the scrollview.
To therefore get a swipe within a scrollview to work, you need to do the following
private void CreateViewHistory()
{
foreach (var i in svHistoryEvents.Subviews)
if (i is UIView)
i.RemoveFromSuperview();
float xtext = 4f;
float y = 4f;
int c = 0;
tbiHistClearAll.Enabled = enableDelete;
foreach (var e in tv)
{
var tgrRemove = new UISwipeGestureRecognizer()
{
NumberOfTouchesRequired = 1,
Direction = UISwipeGestureRecognizerDirection.Right,
};
tgrRemove.AddTarget(this, new Selector("screenSwipe"));
var view = new UIView(new RectangleF(xtext, y, 320f, 90f));
svHistoryEvents.PanGestureRecognizer.RequireGestureRecognizerToFail(tgrRemove);
string info = string.Format("{0} - {1}{2} ({3})\n{4} - {5}\n{6} - {7}\n{8} - {9}", e.EventType, e.Volume, AppDelegate.Self.db.getShortVolumeUnits(), e.Date.ToShortDateString(),
StringUtils.GetString("Sowcrop.Implement"), e.Implement != null ? e.Implement.ImplementType : StringUtils.GetString("Common.NonRecorded"),
StringUtils.GetString("Common.User"), StringUtils.GetString("Common.NonRecorded"),
StringUtils.GetString("Common.Notes"), !string.IsNullOrEmpty(e.Notes) ? e.Notes : StringUtils.GetString("Common.NonRecorded"));
var lbl = new UILabel()
{
UserInteractionEnabled = true
};
lbl = UICreation.MakeLabelWithTag(svHistoryEvents, new RectangleF(0, 0, 320f, 90f), info, UITextAlignment.Left, UIColor.Black, false, 4, c);
view.AddGestureRecognizer(tgrRemove);
view.AddSubview(lbl);
svHistoryEvents.AddSubview(view);
lblTemp.Add(lbl);
c++;
y += 94f;
}
UIUtils.ResizeScrollView(svHistoryEvents);
}
[Export("screenSwipe")]
public void SwipeRemove(UIGestureRecognizer s)
{
var swipe = s as UIGestureRecognizer;
var txt = swipe.View.Subviews[0] as UILabel;
switch (swipe.State)
{
case UIGestureRecognizerState.Began:
Console.WriteLine("Swipe began");
break;
case UIGestureRecognizerState.Changed:
Console.WriteLine("Swipe changed");
break;
case UIGestureRecognizerState.Ended:
Console.WriteLine("Swipe ended");
tv.RemoveAt(txt.Tag);
CreateViewHistory();
break;
case UIGestureRecognizerState.Cancelled:
Console.WriteLine("Swipe cancelled");
break;
}
}
It is not possible to add just a UILabel and have the swipe act on that, it has to be on a UIView with the label as a subview to it.

how to apply different Images to DataGridViewImageColumn in Windows form

I am using DataGridView ,in which I have dynamically created an Image Column ,I want to diplay Pass and fail Images in this column depeending on the condition below is the code,
DataGridViewImageColumn img = new DataGridViewImageColumn();
img.Name = "img";
img.HeaderText = "Image Column";
dataGridView1.DataSource = dt;
dataGridView1.Columns.Add(img);
int number_of_rows = dataGridView1.RowCount;
for (int i = 0; i < (number_of_rows - 1); i++)
{
if (dataGridView1.Rows[i].Cells[2].Value.ToString() == "Pass")
{
Image image = global::Instore.Properties.Resources.pass;
img.Image = image;
dataGridView1.Rows[i].Cells["img"].Value = image;
}
else if (dataGridView1.Rows[i].Cells[2].Value.ToString() == "Fail")
{
Image image2 = global::Instore.Properties.Resources.fail;
img.Image = image2;
dataGridView1.Rows[i].Cells["img"].Value = image2;
}
}
I have attach the code when I am running it its showing Pass.png in all the rows whereas it should show fail image in some of the rows..
Kindly help...
Thanks
Sneha
Remove these two lines:
img.Image = image;
...
img.Image = image2;

Resources